0

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.

Simon
  • 2,328
  • 6
  • 26
  • 30
  • What is on line 349 in Sample.java along with the few lines preceding line 349? – ky1enamic May 25 '12 at 20:02
  • In my code there is: 348. TextView title = (TextView) findViewById(R.id.title); 349. title.setText(R.string.titletext); – Simon May 25 '12 at 20:05

2 Answers2

1

...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.

user
  • 86,916
  • 18
  • 197
  • 190
0

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Yes I thought that too but i double checked them and when I delete R.java and rebuild it the problem won't get away. – Simon May 25 '12 at 19:58
  • And the wrong variables is just in this example. In my origional code it is the right variable. Excuse me for the wrong example. – Simon May 25 '12 at 19:59
  • And then after that, you cleaned the project, restarted eclipse, and/or regenerated the `R.java` class and you are still getting a `NullPointerException`? – Alex Lockwood May 25 '12 at 20:04
  • Can you post your entire `onMenuItemSelected` method and tell us where line 349 is? – Alex Lockwood May 25 '12 at 20:09