Question

I have an option menu in Android. Now it has got 5 options. When I push one button ther comes up an toast, I push another and it starts an activity. That works all fine. Now I want to add another option. When I choose that option on the emulator I want it to change a text, change the image resource of an imageview, and set the visibility of some textviews to View.GONE in another xml and then start the activity whose layout is that xml file. I do the following:

    ...Previous menu options
    case R.id.sample:
    TextView tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setText("This is the new text");
    tv1.setVisibility(View.GONE);
    ImageView iv = (ImageView) findViewById(R.id.iv1);
    iv.setImageResource(R.drawable.image);
    return true;
    following menu options...

When I run it on the emulator and I click this option in the option menu it says:"The application example (process com.android.example) has stopped unexpectedly. Please try again." I already did a project clean, but it doesn't help either. The logcat says:

05-25 19:25:19.357: E/AndroidRuntime(283): FATAL EXCEPTION: main
05-25 19:25:19.357: E/AndroidRuntime(283): java.lang.NullPointerException
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.example.SampleOption.onOptionsItemSelected(Sample.java:349)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.app.Activity.onMenuItemSelected(Activity.java:2195)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.view.View$PerformClick.run(View.java:8816)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.os.Handler.handleCallback(Handler.java:587)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.os.Looper.loop(Looper.java:123)
05-25 19:25:19.357: E/AndroidRuntime(283):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-25 19:25:19.357: E/AndroidRuntime(283):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 19:25:19.357: E/AndroidRuntime(283):  at java.lang.reflect.Method.invoke(Method.java:521)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-25 19:25:19.357: E/AndroidRuntime(283):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-25 19:25:19.357: E/AndroidRuntime(283):  at dalvik.system.NativeStart.main(Native Method)
05-25 19:25:23.127: I/Process(283): Sending signal. PID: 283 SIG: 9

Does anybody know what I'm doing wrong?

Forward thanks.

Was it helpful?

Solution

...and set the visibility of some textviews to View.GONE in another xml and then start the activity whose layout is that xml file.

You can't do this. Searching for the TextView tv1 in the other Activity's(simply using findViewById will search for that TextView in the current Activity's layout) layout will not find the TextView and this will result on tv1 being null. When you'll try to set the text on it it will throw the NullPointerException.

If I understood what are you trying to do, then pass a boolean value in the Intent that you use to start the new Activity and in that Activity's onCreate method check the value of that boolean value from the Intent and then change the visibility of the desired Views.

OTHER TIPS

Change

TextView tv = (TextView) findViewById(R.id.tv1);

to

TextView tv1 = (TextView) findViewById(R.id.tv1);

Edit:

If that doesn't fix the problem, then consider the following scenarios:

  1. If your code is correct, try deleting the class R.java and let Eclipse regenerate it.

  2. If your code is incorrect, then you might want to double check that all of the ids (i.e. R.id.tv1, R.id.tv2, etc.) are defined in the appropriate XML file. If the ids are not found, this would result in a NullPointerException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top