NullPointerException when calling self-written class
I'm calling from one class another class. I can set values and get values when created an object (see Log) but there is lots of methods which throw an exception (see log as well). For example toasting something. If i put the code to toast something in the original class it works, if i try to call it, it doesnt work. In my code below these things which do not work are commented out (// code...
).
Here is the Main.class:
public class Main extends Activity {
private MyMenu myMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMenu = new MyMenu ();
myMenu.setMyMenu (3, "some text");
int number = myMenu.getMyMenuNumber();
String text = myMenu.getMyMenuString();
Log.d("LOG", "number is " + number + " and text is '" + text + "'.");
try {
Log.d ("LOG", "startMain error");
//myMenu.startMain();
startActivity(new Intent (this, Page1.class));
} catch (NullPointerException e) {
e.printStackTrace();
}
try {
Log.d ("LOG", "putToast error");
//myMenu.putToast();
Toast.makeText(
this,
"This is a toast",
Toast.LENGTH_LONG)
.show();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
try {
Log.d ("LOG", "inflateMenu error");
//myMenu.inflateMenu(menu);
getMenuInflater().inflate(R.menu.menu, menu);
} catch (NullPointerException e) {
e.printStackTrace();
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
try {
Log.d ("LOG", "isOptionsButtonClicked error");
//myMenu.isOptionsButtonClicked(item);
switch (item.getItemId()) {
case R.id.feedback:
startActivity(new Intent (this, Page1.class));
break;
case R.id.more_information:
startActivity(new Intent (this, Page1.class));
break;
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return super.onOptionsItemSelected(item);
}
}
and this is MyMenu.class:
public class MyMenu extends Activity {
int number;
String text;
public MyMenu() {
}
public void setMyMenu (int number, String text) {
this.number = number;
this.text = text;
}
public int getMyMenuNumber () {
return number;
}
public String getMyMenuString () {
return text;
}
public void startMain () {
startActivity(new Intent (this, Page1.class));
}
public void inflateMenu (Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
}
public void putToast () {
Toast.makeText(
this,
"This is a toast",
Toast.LENGTH_LONG)
.show();
}
public void isOptionsButtonClicked (MenuItem item) {
switch (item.getItemId()) {
case R.id.feedback:
startActivity(new Intent (this, Page1.class));
break;
case R.id.more_information:
startActivity(new Intent (this, Page1.class));
break;
}
}
}
To stress it out again, running everything in Main.class works perfectly but calling the other classes doesnt work.
UPDATE
Here is parts of the Log/StackTrace:
05-15 08:13:13.502: DEBUG/LOG(546): inflateMenu error
05-15 08:13:13.513: WARN/System.err(546): java.lang.NullPointerException
05-15 08:13:13.522: WARN/System.err(546): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-15 08:13:13.522: WARN/System.err(546): at android.view.MenuInflater.inflate(MenuInflater.java:77)
05-15 08:13:13.522: WARN/System.err(546): at com.test.MyMenu.inflateMenu(MyMenu.java:34)
05-15 08:13:13.533: WARN/System.err(546): at com.test.Main.onCreateOptionsMenu(Main.java:52)
05-15 08:13:13.542: WARN/System.err(546): at android.app.Activity.onCreatePanelMenu(Activity.java:2123)
05-15 08:13:13.542: WARN/System.err(546): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:305)
05-15 08:13:13.542: WARN/System.err(546): at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:550)
05-15 08:13:13.582: WARN/System.err(546): at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1192)
05-15 08:13:13.582: WARN/System.err(546): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1636)
05-15 08:13:13.582: WARN/System.err(546): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
05-15 08:13:13.582: WARN/System.err(546): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
05-15 08:13:13.582: WARN/System.err(546): at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
05-15 08:13:13.582: WARN/System.err(546): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 08:13:13.592: WARN/System.err(546): at android.os.Looper.loop(Looper.java:123)
05-15 08:13:13.592: WARN/System.err(546): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 08:13:13.602: WARN/System.err(546): at java.开发者_StackOverflow中文版lang.reflect.Method.invokeNative(Native Method)
05-15 08:13:13.602: WARN/System.err(546): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 08:13:13.602: WARN/System.err(546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 08:13:13.602: WARN/System.err(546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 08:13:13.602: WARN/System.err(546): at dalvik.system.NativeStart.main(Native Method)
and:
05-15 08:13:03.592: DEBUG/LOG(546): putToast error
05-15 08:13:03.612: WARN/System.err(546): java.lang.NullPointerException
05-15 08:13:03.621: WARN/System.err(546): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
05-15 08:13:03.633: WARN/System.err(546): at android.widget.Toast.<init>(Toast.java:89)
05-15 08:13:03.633: WARN/System.err(546): at android.widget.Toast.makeText(Toast.java:231)
05-15 08:13:03.633: WARN/System.err(546): at com.test.MyMenu.putToast(MyMenu.java:37)
05-15 08:13:03.641: WARN/System.err(546): at com.test.Main.onCreate(Main.java:37)
05-15 08:13:03.641: WARN/System.err(546): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 08:13:03.652: WARN/System.err(546): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-15 08:13:03.652: WARN/System.err(546): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-15 08:13:03.652: WARN/System.err(546): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-15 08:13:03.661: WARN/System.err(546): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-15 08:13:03.661: WARN/System.err(546): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 08:13:03.671: WARN/System.err(546): at android.os.Looper.loop(Looper.java:123)
05-15 08:13:03.671: WARN/System.err(546): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 08:13:03.671: WARN/System.err(546): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 08:13:03.683: WARN/System.err(546): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 08:13:03.683: WARN/System.err(546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 08:13:03.691: WARN/System.err(546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 08:13:03.691: WARN/System.err(546): at dalvik.system.NativeStart.main(Native Method)
hope that helps!
UDATE 2
Here is the requested Page1.class. But again everything runs fine when i do not use the MyMenu.class.
public class Page1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
}
}
Furthermore you can download the android project from microsoft's/windows live skydrive: http://cid-f45ce92851885387.office.live.com/browse.aspx/Stackoverflow
I can't figure out why you're delegating this method below to other activity:
public void startMain () {
startActivity(new Intent (this, Page1.class)); // this <- here is problem
}
I suppose that's because of bad desing. Don't do that. Use Intents. Pass data between Activities in Intents in Bundles. In your case you didn't pass Context to MyMenu Activity. MyMenu hasn't got instance of Application Context therefore you're getting NPE.
response for comment:
You can do static startMain method, of course. But pay attention to what is 'this' in new Intent(this, Page1.class). You should pass Context of Activity from which you're starting the new Activity.
public static void startMain (Activity context) {
context.startActivity(new Intent (context, Page1.class));
}
精彩评论