shadi
04-23-2011, 03:22 AM
hi,
i am doing coding for android using eclipse. i am trying to do simple intent to go from one activity to another. i did the 2 xml files and the 2 java files. i put a button in both and set up the intents. the code compiled ok. i ran it. the android emulator launched. i click on the button to go to the other screen....and....i got the force close error and my application exited:confused:
this is the xml file of main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WELCOME TO"
android:textSize="18sp"
android:textStyle="bold"
android:layout_x="90px"
android:layout_y="28px"
>
</TextView>
<TextView
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THE"
android:textSize="18sp"
android:textStyle="bold"
android:layout_x="130px"
android:layout_y="52px"
>
</TextView>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDERING"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="109px"
android:layout_y="78px"
>
</TextView>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APPLICATION"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="99px"
android:layout_y="104px"
>
</TextView>
<Button
android:id="@+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="help"
android:layout_x="275px"
android:layout_y="7px"
>
</Button>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_x="132px"
android:layout_y="148px"
>
</Button>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="settings"
android:layout_x="122px"
android:layout_y="213px"
>
</Button>
<Button
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check order status"
android:layout_x="89px"
android:layout_y="273px"
>
</Button>
<Button
android:id="@+id/quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="quit"
android:layout_x="135px"
android:layout_y="332px"
>
</Button>
</AbsoluteLayout>
this is the java file for it:-
//code for welcome screen
package com.pizzaAPP;
import android.app.Activity;
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;
public class main_screen extends Activity implements OnClickListener
{
Button help;
Button settings;
Button next;
Button status;
Button quit;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
help = (Button)findViewById(R.id.help);
settings = (Button)findViewById(R.id.settings);
next = (Button)findViewById(R.id.next);
status = (Button)findViewById(R.id.status);
quit = (Button)findViewById(R.id.quit);
//listening to clicks
help.setOnClickListener(this);
settings.setOnClickListener(this);
next.setOnClickListener(this);
status.setOnClickListener(this);
quit.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId()==help.getId())
{
Toast.makeText(getApplicationContext(), "this is the welcome screen. clicking next goes to the main screen of the application, clicking settings goes to the settings screen, clicking check order status goes to a screen in which you can check the order status and finally clicking quit exits the application", Toast.LENGTH_LONG).show();
}
if (v.getId()==settings.getId()){
startActivity(new Intent(this, screen_9.class));
}
if (v.getId()==next.getId() )
{
startActivity(new Intent(this, screen_1.class));
}
if (v.getId()==status.getId()) {
startActivity(new Intent(this, screen_10.class));
}
if (v.getId()==quit.getId()) {onStop();}
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
as you can see there are 3 intents:- the next button, the settings button and the status button. the next button works because the intent for it was added for me. i added the intent for status and settings by creating the xml and java for them
here is the xml file for the settings screen:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SETTINGS SCREEN"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="95px"
android:layout_y="13px"
>
</TextView>
<Button
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c.menu"
android:layout_x="258px"
android:layout_y="6px"
>
</Button>
<Button
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="130px"
android:layout_y="374px"
>
</Button>
</AbsoluteLayout>
and the java file for it:-
//code for settings screen
package com.pizzaAPP;
import android.app.Activity;
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 android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
public class screen_9 extends Activity implements OnClickListener {
//java variables declared
Button menu;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_9);
//bind java variables to xml
menu = (Button)findViewById(R.id.menu);
back = (Button)findViewById(R.id.back);
//listen to buttons
registerForContextMenu(menu);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==back.getId()) {startActivity(new Intent(this, main_screen.class));}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("the application's menu");
menu.add(0, v.getId(), 0, "go to settings");
menu.add(0, v.getId(), 0, "help");
menu.add(0, v.getId(), 0, "quit application");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="go to settings"){function1(item.getItemId());}
else if(item.getTitle()=="help"){function2(item.getItemId());}
else if(item.getTitle()=="quit application"){function3(item.getItemId());}
else {return false;}
return true;
}
private void function1(int itemId) {
// TODO Auto-generated method stub
}
private void function2(int itemId) {
Toast.makeText(this, "this is the settings screen. back takes you back a screen", Toast.LENGTH_LONG).show();
}
private void function3(int itemId) {
onStop();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
here is the xml file for screen_1:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WHAT DO"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="118px"
android:layout_y="26px"
>
</TextView>
<TextView
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOU WANT"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="113px"
android:layout_y="58px"
>
</TextView>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TO DO?"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="125px"
android:layout_y="84px"
>
</TextView>
<Button
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c.menu"
android:layout_x="258px"
android:layout_y="6px"
>
</Button>
<Button
android:id="@+id/vmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view menus"
android:layout_x="118px"
android:layout_y="134px"
>
</Button>
<Button
android:id="@+id/voffers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view special offers"
android:layout_x="97px"
android:layout_y="187px"
>
</Button>
<Button
android:id="@+id/morder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="make your order"
android:layout_x="102px"
android:layout_y="238px"
>
</Button>
<Button
android:id="@+id/basket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view basket and purchase"
android:layout_x="73px"
android:layout_y="289px"
>
</Button>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_x="139px"
android:layout_y="343px"
>
</Button>
</AbsoluteLayout>
here is the java file for it:-
//code for main screen
package com.pizzaAPP;
import android.app.Activity;
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 android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
public class screen_1 extends Activity implements OnClickListener {
//java variables declared
Button menu;
Button vmenu;
Button voffers;
Button morder;
Button basket;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_1);
//bind java variables to xml
menu = (Button)findViewById(R.id.menu);
vmenu = (Button)findViewById(R.id.vmenu);
voffers = (Button)findViewById(R.id.voffers);
morder = (Button)findViewById(R.id.morder);
basket = (Button)findViewById(R.id.basket);
back = (Button)findViewById(R.id.back);
//listen to buttons
registerForContextMenu(menu);
vmenu.setOnClickListener(this);
voffers.setOnClickListener(this);
morder.setOnClickListener(this);
basket.setOnClickListener(this);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==vmenu.getId()) {startActivity(new Intent(this, screen_2.class));}
if (v.getId()==voffers.getId()) {startActivity(new Intent(this, screen_8.class));}
if (v.getId()==morder.getId()) {startActivity(new Intent(this, screen_3.class));}
if (v.getId()==basket.getId()) {startActivity(new Intent(this, screen_11.class));}
if (v.getId()==back.getId()) {startActivity(new Intent(this, main_screen.class));}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("the application's menu");
menu.add(0, v.getId(), 0, "go to settings");
menu.add(0, v.getId(), 0, "help");
menu.add(0, v.getId(), 0, "quit application");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="go to settings"){function1(item.getItemId());}
else if(item.getTitle()=="help"){function2(item.getItemId());}
else if(item.getTitle()=="quit application"){function3(item.getItemId());}
else {return false;}
return true;
}
private void function1(int itemId) {
// TODO Auto-generated method stub
}
private void function2(int itemId) {
Toast.makeText(this, "in this screen you can view the menus and the special offers the shop has to offer. once you are satisfied you can click on make order and inside there you can add to basket. to see and purchase the items in the basket click on view basket and purchase. clicking back goes to the welcome screen", Toast.LENGTH_LONG).show();
}
private void function3(int itemId) {
onStop();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
i have a total of 14 screens in my project. screen_4 to screen_14 are all kind of similar for now. just copy and paste. the difference is in number of buttons in each screen. only going between main, screen_1 to screen_3 work. the project file was like this as it was given to me. i added screen_4 to screen_14 and get force close when i try to go to them
here is the manifest file:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pizzaAPP"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".main_screen"
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=".screen_1" android:label="@string/app_name" />
<activity android:name=".screen_2" android:label="@string/app_name" />
<activity android:name=".screen_3" android:label="@string/app_name" />
<activity android:name=".screen_4" android:label="@string/app_name" />
<activity android:name=".screen_5" android:label="@string/app_name" />
<activity android:name=".screen_6" android:label="@string/app_name" />
<activity android:name=".screen_7" android:label="@string/app_name" />
<activity android:name=".screen_8" android:label="@string/app_name" />
<activity android:name=".screen_9" android:label="@string/app_name" />
<activity android:name=".screen_10" android:label="@string/app_name" />
<activity android:name=".screen_11" android:label="@string/app_name" />
<activity android:name=".screen_12" android:label="@string/app_name" />
<activity android:name=".screen_13" android:label="@string/app_name" />
<activity android:name=".screen_14" android:label="@string/app_name" />
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
i was told i had to add the line <activity android:name=".xyz" android:label="@string/app_name" /> in the manifest file for each new screen i add where xyz is the screen name in order for it not to give force close because without it then it will. its done for screen_4 to screen_14 and it still gives me force close
here is R.java:-
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.pizzaAPP;
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 back=0x7f05000f;
public static final int basket=0x7f05000e;
public static final int dessert=0x7f050014;
public static final int drinks=0x7f050013;
public static final int help=0x7f050005;
public static final int menu=0x7f05000a;
public static final int morder=0x7f05000d;
public static final int name1=0x7f050015;
public static final int name10=0x7f050027;
public static final int name11=0x7f050029;
public static final int name12=0x7f05002b;
public static final int name13=0x7f05002d;
public static final int name14=0x7f05002f;
public static final int name2=0x7f050017;
public static final int name3=0x7f050019;
public static final int name4=0x7f05001b;
public static final int name5=0x7f05001d;
public static final int name6=0x7f05001f;
public static final int name7=0x7f050021;
public static final int name8=0x7f050023;
public static final int name9=0x7f050025;
public static final int next=0x7f050006;
public static final int offer1=0x7f050031;
public static final int offer2=0x7f050032;
public static final int offer3=0x7f050033;
public static final int offer4=0x7f050034;
public static final int offer5=0x7f050035;
public static final int offer6=0x7f050036;
public static final int offer7=0x7f050037;
public static final int offer8=0x7f050038;
public static final int pizza=0x7f050011;
public static final int price1=0x7f050016;
public static final int price10=0x7f050028;
public static final int price11=0x7f05002a;
public static final int price12=0x7f05002c;
public static final int price13=0x7f05002e;
public static final int price14=0x7f050030;
public static final int price2=0x7f050018;
public static final int price3=0x7f05001a;
public static final int price4=0x7f05001c;
public static final int price5=0x7f05001e;
public static final int price6=0x7f050020;
public static final int price7=0x7f050022;
public static final int price8=0x7f050024;
public static final int price9=0x7f050026;
public static final int quit=0x7f050009;
public static final int settings=0x7f050007;
public static final int status=0x7f050008;
public static final int tlist=0x7f050012;
public static final int vmenu=0x7f05000b;
public static final int voffers=0x7f05000c;
public static final int widget0=0x7f050000;
public static final int widget29=0x7f050001;
public static final int widget30=0x7f050002;
public static final int widget31=0x7f050003;
public static final int widget32=0x7f050004;
public static final int widget44=0x7f050010;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int screen_1=0x7f030001;
public static final int screen_10=0x7f030002;
public static final int screen_11=0x7f030003;
public static final int screen_12=0x7f030004;
public static final int screen_13=0x7f030005;
public static final int screen_14=0x7f030006;
public static final int screen_2=0x7f030007;
public static final int screen_3=0x7f030008;
public static final int screen_4=0x7f030009;
public static final int screen_5=0x7f03000a;
public static final int screen_6=0x7f03000b;
public static final int screen_7=0x7f03000c;
public static final int screen_8=0x7f03000d;
public static final int screen_9=0x7f03000e;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
why is it giving me force close in some screens and in others no? who is to blame? is it my code or is it the fault of the android emulator? i am unsure which revision the sdk is, either 8 or 9. the android is 2.3.1. attached is the zip file that has the complete project
i am doing coding for android using eclipse. i am trying to do simple intent to go from one activity to another. i did the 2 xml files and the 2 java files. i put a button in both and set up the intents. the code compiled ok. i ran it. the android emulator launched. i click on the button to go to the other screen....and....i got the force close error and my application exited:confused:
this is the xml file of main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WELCOME TO"
android:textSize="18sp"
android:textStyle="bold"
android:layout_x="90px"
android:layout_y="28px"
>
</TextView>
<TextView
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THE"
android:textSize="18sp"
android:textStyle="bold"
android:layout_x="130px"
android:layout_y="52px"
>
</TextView>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ORDERING"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="109px"
android:layout_y="78px"
>
</TextView>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APPLICATION"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="99px"
android:layout_y="104px"
>
</TextView>
<Button
android:id="@+id/help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="help"
android:layout_x="275px"
android:layout_y="7px"
>
</Button>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_x="132px"
android:layout_y="148px"
>
</Button>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="settings"
android:layout_x="122px"
android:layout_y="213px"
>
</Button>
<Button
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check order status"
android:layout_x="89px"
android:layout_y="273px"
>
</Button>
<Button
android:id="@+id/quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="quit"
android:layout_x="135px"
android:layout_y="332px"
>
</Button>
</AbsoluteLayout>
this is the java file for it:-
//code for welcome screen
package com.pizzaAPP;
import android.app.Activity;
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;
public class main_screen extends Activity implements OnClickListener
{
Button help;
Button settings;
Button next;
Button status;
Button quit;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
help = (Button)findViewById(R.id.help);
settings = (Button)findViewById(R.id.settings);
next = (Button)findViewById(R.id.next);
status = (Button)findViewById(R.id.status);
quit = (Button)findViewById(R.id.quit);
//listening to clicks
help.setOnClickListener(this);
settings.setOnClickListener(this);
next.setOnClickListener(this);
status.setOnClickListener(this);
quit.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId()==help.getId())
{
Toast.makeText(getApplicationContext(), "this is the welcome screen. clicking next goes to the main screen of the application, clicking settings goes to the settings screen, clicking check order status goes to a screen in which you can check the order status and finally clicking quit exits the application", Toast.LENGTH_LONG).show();
}
if (v.getId()==settings.getId()){
startActivity(new Intent(this, screen_9.class));
}
if (v.getId()==next.getId() )
{
startActivity(new Intent(this, screen_1.class));
}
if (v.getId()==status.getId()) {
startActivity(new Intent(this, screen_10.class));
}
if (v.getId()==quit.getId()) {onStop();}
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
as you can see there are 3 intents:- the next button, the settings button and the status button. the next button works because the intent for it was added for me. i added the intent for status and settings by creating the xml and java for them
here is the xml file for the settings screen:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SETTINGS SCREEN"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="95px"
android:layout_y="13px"
>
</TextView>
<Button
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c.menu"
android:layout_x="258px"
android:layout_y="6px"
>
</Button>
<Button
android:id="@+id/widget44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_x="130px"
android:layout_y="374px"
>
</Button>
</AbsoluteLayout>
and the java file for it:-
//code for settings screen
package com.pizzaAPP;
import android.app.Activity;
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 android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
public class screen_9 extends Activity implements OnClickListener {
//java variables declared
Button menu;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_9);
//bind java variables to xml
menu = (Button)findViewById(R.id.menu);
back = (Button)findViewById(R.id.back);
//listen to buttons
registerForContextMenu(menu);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==back.getId()) {startActivity(new Intent(this, main_screen.class));}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("the application's menu");
menu.add(0, v.getId(), 0, "go to settings");
menu.add(0, v.getId(), 0, "help");
menu.add(0, v.getId(), 0, "quit application");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="go to settings"){function1(item.getItemId());}
else if(item.getTitle()=="help"){function2(item.getItemId());}
else if(item.getTitle()=="quit application"){function3(item.getItemId());}
else {return false;}
return true;
}
private void function1(int itemId) {
// TODO Auto-generated method stub
}
private void function2(int itemId) {
Toast.makeText(this, "this is the settings screen. back takes you back a screen", Toast.LENGTH_LONG).show();
}
private void function3(int itemId) {
onStop();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
here is the xml file for screen_1:-
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WHAT DO"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="118px"
android:layout_y="26px"
>
</TextView>
<TextView
android:id="@+id/widget30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOU WANT"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="113px"
android:layout_y="58px"
>
</TextView>
<TextView
android:id="@+id/widget31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TO DO?"
android:textSize="16sp"
android:textStyle="bold"
android:layout_x="125px"
android:layout_y="84px"
>
</TextView>
<Button
android:id="@+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="c.menu"
android:layout_x="258px"
android:layout_y="6px"
>
</Button>
<Button
android:id="@+id/vmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view menus"
android:layout_x="118px"
android:layout_y="134px"
>
</Button>
<Button
android:id="@+id/voffers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view special offers"
android:layout_x="97px"
android:layout_y="187px"
>
</Button>
<Button
android:id="@+id/morder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="make your order"
android:layout_x="102px"
android:layout_y="238px"
>
</Button>
<Button
android:id="@+id/basket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="view basket and purchase"
android:layout_x="73px"
android:layout_y="289px"
>
</Button>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:layout_x="139px"
android:layout_y="343px"
>
</Button>
</AbsoluteLayout>
here is the java file for it:-
//code for main screen
package com.pizzaAPP;
import android.app.Activity;
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 android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
public class screen_1 extends Activity implements OnClickListener {
//java variables declared
Button menu;
Button vmenu;
Button voffers;
Button morder;
Button basket;
Button back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_1);
//bind java variables to xml
menu = (Button)findViewById(R.id.menu);
vmenu = (Button)findViewById(R.id.vmenu);
voffers = (Button)findViewById(R.id.voffers);
morder = (Button)findViewById(R.id.morder);
basket = (Button)findViewById(R.id.basket);
back = (Button)findViewById(R.id.back);
//listen to buttons
registerForContextMenu(menu);
vmenu.setOnClickListener(this);
voffers.setOnClickListener(this);
morder.setOnClickListener(this);
basket.setOnClickListener(this);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==vmenu.getId()) {startActivity(new Intent(this, screen_2.class));}
if (v.getId()==voffers.getId()) {startActivity(new Intent(this, screen_8.class));}
if (v.getId()==morder.getId()) {startActivity(new Intent(this, screen_3.class));}
if (v.getId()==basket.getId()) {startActivity(new Intent(this, screen_11.class));}
if (v.getId()==back.getId()) {startActivity(new Intent(this, main_screen.class));}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("the application's menu");
menu.add(0, v.getId(), 0, "go to settings");
menu.add(0, v.getId(), 0, "help");
menu.add(0, v.getId(), 0, "quit application");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="go to settings"){function1(item.getItemId());}
else if(item.getTitle()=="help"){function2(item.getItemId());}
else if(item.getTitle()=="quit application"){function3(item.getItemId());}
else {return false;}
return true;
}
private void function1(int itemId) {
// TODO Auto-generated method stub
}
private void function2(int itemId) {
Toast.makeText(this, "in this screen you can view the menus and the special offers the shop has to offer. once you are satisfied you can click on make order and inside there you can add to basket. to see and purchase the items in the basket click on view basket and purchase. clicking back goes to the welcome screen", Toast.LENGTH_LONG).show();
}
private void function3(int itemId) {
onStop();
}
@Override
protected void onStop() {
super.onStop();
this.finish();
}
}
i have a total of 14 screens in my project. screen_4 to screen_14 are all kind of similar for now. just copy and paste. the difference is in number of buttons in each screen. only going between main, screen_1 to screen_3 work. the project file was like this as it was given to me. i added screen_4 to screen_14 and get force close when i try to go to them
here is the manifest file:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pizzaAPP"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".main_screen"
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=".screen_1" android:label="@string/app_name" />
<activity android:name=".screen_2" android:label="@string/app_name" />
<activity android:name=".screen_3" android:label="@string/app_name" />
<activity android:name=".screen_4" android:label="@string/app_name" />
<activity android:name=".screen_5" android:label="@string/app_name" />
<activity android:name=".screen_6" android:label="@string/app_name" />
<activity android:name=".screen_7" android:label="@string/app_name" />
<activity android:name=".screen_8" android:label="@string/app_name" />
<activity android:name=".screen_9" android:label="@string/app_name" />
<activity android:name=".screen_10" android:label="@string/app_name" />
<activity android:name=".screen_11" android:label="@string/app_name" />
<activity android:name=".screen_12" android:label="@string/app_name" />
<activity android:name=".screen_13" android:label="@string/app_name" />
<activity android:name=".screen_14" android:label="@string/app_name" />
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
i was told i had to add the line <activity android:name=".xyz" android:label="@string/app_name" /> in the manifest file for each new screen i add where xyz is the screen name in order for it not to give force close because without it then it will. its done for screen_4 to screen_14 and it still gives me force close
here is R.java:-
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.pizzaAPP;
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 back=0x7f05000f;
public static final int basket=0x7f05000e;
public static final int dessert=0x7f050014;
public static final int drinks=0x7f050013;
public static final int help=0x7f050005;
public static final int menu=0x7f05000a;
public static final int morder=0x7f05000d;
public static final int name1=0x7f050015;
public static final int name10=0x7f050027;
public static final int name11=0x7f050029;
public static final int name12=0x7f05002b;
public static final int name13=0x7f05002d;
public static final int name14=0x7f05002f;
public static final int name2=0x7f050017;
public static final int name3=0x7f050019;
public static final int name4=0x7f05001b;
public static final int name5=0x7f05001d;
public static final int name6=0x7f05001f;
public static final int name7=0x7f050021;
public static final int name8=0x7f050023;
public static final int name9=0x7f050025;
public static final int next=0x7f050006;
public static final int offer1=0x7f050031;
public static final int offer2=0x7f050032;
public static final int offer3=0x7f050033;
public static final int offer4=0x7f050034;
public static final int offer5=0x7f050035;
public static final int offer6=0x7f050036;
public static final int offer7=0x7f050037;
public static final int offer8=0x7f050038;
public static final int pizza=0x7f050011;
public static final int price1=0x7f050016;
public static final int price10=0x7f050028;
public static final int price11=0x7f05002a;
public static final int price12=0x7f05002c;
public static final int price13=0x7f05002e;
public static final int price14=0x7f050030;
public static final int price2=0x7f050018;
public static final int price3=0x7f05001a;
public static final int price4=0x7f05001c;
public static final int price5=0x7f05001e;
public static final int price6=0x7f050020;
public static final int price7=0x7f050022;
public static final int price8=0x7f050024;
public static final int price9=0x7f050026;
public static final int quit=0x7f050009;
public static final int settings=0x7f050007;
public static final int status=0x7f050008;
public static final int tlist=0x7f050012;
public static final int vmenu=0x7f05000b;
public static final int voffers=0x7f05000c;
public static final int widget0=0x7f050000;
public static final int widget29=0x7f050001;
public static final int widget30=0x7f050002;
public static final int widget31=0x7f050003;
public static final int widget32=0x7f050004;
public static final int widget44=0x7f050010;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int screen_1=0x7f030001;
public static final int screen_10=0x7f030002;
public static final int screen_11=0x7f030003;
public static final int screen_12=0x7f030004;
public static final int screen_13=0x7f030005;
public static final int screen_14=0x7f030006;
public static final int screen_2=0x7f030007;
public static final int screen_3=0x7f030008;
public static final int screen_4=0x7f030009;
public static final int screen_5=0x7f03000a;
public static final int screen_6=0x7f03000b;
public static final int screen_7=0x7f03000c;
public static final int screen_8=0x7f03000d;
public static final int screen_9=0x7f03000e;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
why is it giving me force close in some screens and in others no? who is to blame? is it my code or is it the fault of the android emulator? i am unsure which revision the sdk is, either 8 or 9. the android is 2.3.1. attached is the zip file that has the complete project