Android onClickListener - java

SOLVED: Put my file in the wrong folder, my stupid mistake :)
Anyone knows why this won't work? It just doesn't do anything. I have everything in the Main XML defined properly i think, but i feel like something is missing here.
I am new to Java/Android developing but i was using VB.NET for a long time.
package com.example.myfirstapplication;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class Main extends Activity{
int counter;
Button add, sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAddOne);
sub = (Button) findViewById(R.id.bSubOne);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
display.setText("Your total is " + counter);
}
});
}
}
Thanks in advance.
EDIT: Added layout. Thought it wasn't needed:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:layout_gravity="center"
android:textSize="45dp"
android:id="#+id/tvDisplay"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add 1"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bAddOne" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract 1"
android:layout_gravity="center"
android:textSize="20dp"
android:id="#+id/bSubOne"/>
</LinearLayout>
Edit: And the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Ok. A quick fix is to change the name of the Activity from Main.java to MainActivity.java.
package com.example.myfirstapplication;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity{
int counter;
Button add, sub;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAddOne);
sub = (Button) findViewById(R.id.bSubOne);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter--;
display.setText("Your total is " + counter);
}
});
}
}
The reason is That in the AndroidManifest.xml the starting Activity is declared as MainActivity:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It is in the line that says: android:name=".MainActivity", and the
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
says that it is the starting and main activity.

Related

How to solve the error in the AndroidManifest file "Unresolved class 'MainActivity' "?

My AndroidManifest file is showing an error "Unresolved class 'MainActivity' ", but i have a java class and file by that name in the package and it is supposed to be the first activity of the program so when i try to run the program it crashes up , i've checked my code but couldn't find any problem there .
java code for MainActivity file
package com.example.tictactoe;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button forward;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forward = (Button)findViewById(R.id.start);
forward.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this,GameUi.class));
}
});
}
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to Exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
java code for second activity which my program uses ("GameUi.java")
package com.example.tictactoe
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class GameUi extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_ui);
btn=(Button)findViewById(R.id.backbtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(GameUi.this, MainActivity.class));
}
});
}
}
AndroidManifest file code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tictactoe">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TicTacToe">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameUi">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity xml file code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E5E6AD"
tools:context=".MainActivity">
<TextView
android:id="#+id/Heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="332dp"
android:editable="false"
android:text="Hello Aliens!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="50dp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="16dp" />
<ImageView
android:id="#+id/Alienimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/Heading"
app:srcCompat="#drawable/ic_launcher_foreground" />
<Button
android:id="#+id/Startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000066"
android:letterSpacing="0.5"
android:padding="0dp"
android:text="Start"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Alienimg"
app:layout_constraintVertical_bias="0.886" />
</androidx.constraintlayout.widget.ConstraintLayout>
In your class's I don't see the package at top.
Try adding it and see, if you still face the issue, try invalidate and restarting.
package com.example.tictactoe;
This is the cause of error
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameUi">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
the intent-filter is used to define that which activity will be your first activity ( when you start your Android Application ). You can use it in both activities so the App will confuse which is your first Activity so they give error.
In case MainActivity is your first activity
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameUi">
</activity>
Your manifest is Ok ( I guess you fixed the intent-filter issue based on
Abdullah Sheikh answer). the problem is because of this line in MainActivity :
forward = (Button)findViewById(R.id.start);
because your button id is Startbtn not start
just replace that with :
forward = (Button)findViewById(R.id.Startbtn);
also in activity_main and ImageView remove
app:srcCompat="#drawable/ic_launcher_foreground"
or find another src for drawable.

All Buttons with Intent Don't work

Goal:
When I press one of the buttons, you should be referred into a next intent page.
Problem:
It only works for button named "Activity 1" but not the remaining buttons although you use the same source code for button "Activity 1", "Activity 2" and "Activity 3".
What part am I missing?
Thank you!
Info:
*I'm new in android
*I'm using API 23
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".activity2">
</activity>
</application>
</manifest>
MainActivity.java
package com.jfdimarzio.application;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static android.R.attr.value;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if(intent != null)
{
String valuee = intent.getStringExtra("key");
if(valuee != null)
{
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
}
public void onClick(View view)
{
switch(view.getId())
{
case R.id.btn_activity1:
Intent myIntent1 = new Intent(getBaseContext(), firstactivity.class );
myIntent1.putExtra("key", "Hello! Activity 1");
startActivity(myIntent1);
break;
case R.id.btn_activity2:
Intent myIntent2 = new Intent(getBaseContext(), Activity2.class );
myIntent2.putExtra("key", "Hello! Activity 2");
startActivity(myIntent2);
break;
case R.id.btn_activity3:
Intent myIntent3 = new Intent(getBaseContext(), Activity3.class );
myIntent3.putExtra("key", "Hello! Activity 3");
startActivity(myIntent3);
break;
}
}
}
firstactivity.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class firstactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity3.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity3 extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
Activity2.java
package com.jfdimarzio.application;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Activity2 extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String valuee = intent.getStringExtra("key");
final TextView textViewToChange = (TextView) findViewById(R.id.txtvw_activity1);
textViewToChange.setText(valuee);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jfdimarzio.application.MainActivity">
<TextView
android:id="#+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/txtvw_first"
android:layout_marginTop="67dp"
android:onClick="onClick"
android:text="Activity 1" />
<Button
android:id="#+id/btn_activity2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_activity1"
android:layout_marginTop="15dp"
android:onClick="onClick"
android:text="Activity 2" />
<Button
android:id="#+id/btn_activity3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/btn_activity2"
android:layout_marginTop="19dp"
android:onClick="onClick"
android:text="Activity 3" />
</RelativeLayout>
firstactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt_message"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtvw_activity1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
</RelativeLayout>
activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
Looks like you have not declared all the activities in your Manifest File. Make sure you include all the activities in your Manifest.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jfdimarzio.application">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".firstactivity">
</activity>
<activity android:name=".Activity2">
</activity>
<activity android:name=".Activity3">
</activity>
</application>
you just need to declare all the activites in the manifest file and name of the file in the
<activity android:name="...."></activity> tag must to similar to the name of your .java class file name.
Your entry of Activity2.java class is
<activity android:name=".activity2">
</activity>
It should be like this
<activity android:name=".Activity2">
</activity>

Cannot resolve symbol 'R' error in android studio

I already have tried Cleaning the project and rebuilding it also syncing project with grade files and invalidate caches / restart.
But still I am facing this error.
P.S. I am very new to android programming.
here is my Android Manifest file .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shubhangkhattar.newboston" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.shubhangkhattar.newboston.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.TEXTPLAY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my MainActivity.java
package com.example.shubhangkhattar.newboston;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("Your Total is " + counter );
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("Your Total is" + counter);
}
});
}
}
Here is my activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:text="Your Total Is 0"
android:layout_width="fill_parent"
android:layout_height="wrap_ content"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="20dp"
/>
<Button
android:id="#+id/bSub"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="20dp"
/>
</LinearLayout>
This is what my error build gradle shows.
:app:processDebugResources
/Users/shubhangkhattar/AndroidStudioProjects/NewBoston/app/src/main/res/layout/activity_main.xml
Error:(11, 32) String types not allowed (at 'layout_height' with value 'wrap_ content').
/Users/shubhangkhattar/AndroidStudioProjects/NewBoston/app/src/main/res/layout/text.xml
Error:(16) No resource identifier found for attribute 'layout_orientation' in package 'android'
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/shubhangkhattar/Library/Android/sdk/build-tools/22.0.1/aapt'' finished with non-zero exit value 1
You should import your R file in MainActivity.java
import com.example.shubhangkhattar.newboston.R;
Try to press alt + Enter on highlighted R symbol.
Create another Class "R"
package com.example.asus.yourname;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int b1=0x7f050001;
public static final int text1=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int app_name1=0x7f040003;
public static final int hello=0x7f040000;
public static final int hello1=0x7f040002;
}
}

When I press back on my program i get a blank screen [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After getting to the chapter3_1.xml page which pulls up fine when i press my phones back key and it takes me to the previous button. press back again and there's a blank page that isn't supposed to be there. press back again and you get to the original button. no errors no crashes.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.th3ramr0d.ar670_1quickreference"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SubMenu1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.SUBMENU1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.CHAPTER3" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Chapter3_1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.th3ramr0d.ar670_1quickreference.CHAPTER3_1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainMenu.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainMenu extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnChpt3 = (Button) findViewById(R.id.btnChpt3);
btnChpt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.CHAPTER3"));
}
});
}
}
SubMenu1.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SubMenu1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3);
Button btnChpt3 = (Button) findViewById(R.id.btnChpt3_1);
btnChpt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.CHAPTER3_1"));
}
});
}
}
Chapter3.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Chapter3 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
startActivity(new Intent("com.th3ramr0d.ar670_1quickreference.SUBMENU1"));
}
}
Chapter3_1.java
package com.th3ramr0d.ar670_1quickreference;
import android.app.Activity;
import android.os.Bundle;
public class Chapter3_1 extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chapter3_1);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.th3ramr0d.ar670_1quickreference.MainActivity" >
<Button
android:id="#+id/btnChpt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter 3" />
</LinearLayout>
chapter3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnChpt3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fuckmylife" />
</LinearLayout>
chapter3_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the content of chapter 3_1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I think you are using fragment in your application.
And when you pushing the fragment you'r are adding it to back stack thats why when you press back button it will show a blank screen which is the framelayout on which you are showing the fragment view.
Refer the link :-
Fragmnet manager
In your manifest, in activity you should set parent like this:
android:parentActivityName="com.some.myproject.ParentActivityToThisActivity"
But it can not working with API 14, API 15 should be used or newest.

I cannot get Google Maps API to work on my android Application (V2)

In main2.xml there are apparently no errors. Same thing for main.xml however I think the problem lies here somewhere.
Quick Summary of what the app is: It is meant to use Google Maps onClick of a button but I am getting errors so I can just load the main menu on testing.
XML
I don't have enough rep to post picture so error pic can be found here
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Club Deals"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GPS locations" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Closest Deals"
android:onClick="open_close_deals" />
<Button
android:id="#+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cheapest Deals"
android:onClick="open_cheap_deals" />
<Button
android:id="#+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Best Value Deals"
android:onClick="best_value" />
<Button
android:id="#+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Best Events"
android:onClick="best_events" />
<Button
android:id="#+id/button9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="News"
android:onClick="news"/>
</LinearLayout>
AppActivity.java
In AppActivity I am given
"map cannot be resolved or is not a field" & "main2 cannot be resolved or is not a field"
package com.example.clubnightsdeals;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
}
});
};
public void addListenerOnButtonNews() {
final Context context = this;
button = (Button) findViewById(R.id.button9);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
}
});
};
/* protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
*/
// Google Map
GoogleMap googleMap;
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
In App2Activity I am given the error "The import com.example.clubnightsdeals.R cannot be resolved"
App2Activity.java
package com.example.clubnightsdeals;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import com.example.clubnightsdeals.R;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clubnightsdeals"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="info.androidhive.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/clubnightsdeals"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library
android:name="com.google.android.maps" />
<activity
android:label="#string/clubnightsdeals"
android:name=".App2Activity" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDetfYqNFORnvtR245fht3RIK25T6DRY" />
  <activity
            android:name="info.androidhive.googlemapsv2.MainActivity"
            android:label="#string/app_name"
            android:theme="#style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
</manifest>
You are pointing to wrong R.java file. Replace your following import line
import android.R;
with
import com.example.clubnightsdeals.R;
in your AppActivity.java file.
Your error shows that you have not closed the <activity> tag in your manifest file.
Make sure you have closed your activity tag as <activity android:name=""...../> or <activity android:name=""> </activity>
First remove
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
in manifest (cannot have 2 start activity in a App) then try to import R library.
If it still has this R error, check your xml files - make sure check all xml files.

Categories