调用自编写的类时出现NullPointerException

时间:2011-05-15 07:53:31

标签: java android nullpointerexception

我正在从一个班级打电话给另一个班级。我可以在创建对象时设置值并获取值(请参阅日志),但是有很多方法会引发异常(请参阅日志)。例如烘烤一些东西。如果我把代码放在原始类中的东西,它可以工作,如果我尝试调用它,它不起作用。在下面的代码中,这些不起作用的东西被注释掉了(// code...)。

这是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);
}
}

这是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;
    }
}
}

要再次强调它,在Main.class中运行所有内容都可以正常工作,但调用其他类不起作用。

更新

以下是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.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)

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)
希望有所帮助!

UDATE 2

这是请求的Page1.class。但是当我不使用MyMenu.class时,一切都运行良好。

public class Page1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page1);
}

}

此外,您可以从microsoft的/ windows live skydrive下载android项目: http://cid-f45ce92851885387.office.live.com/browse.aspx/Stackoverflow

1 个答案:

答案 0 :(得分:0)

我无法弄清楚为什么要将此方法委托给其他活动:

public void startMain () {
   startActivity(new Intent (this, Page1.class)); // this <- here is problem
}

我想这是因为设计不好。不要那样做。使用意图。在Bundles中的Intents中的活动之间传递数据。 在您的情况下,您没有将Context传递给MyMenu活动。 MyMenu没有应用程序上下文的实例,因此你得到了NPE。

评论回复:
当然,你可以做静态startMain方法。但要注意新Intent(this,Page1.class)中的'this'是什么。您应该传递您正在开始新活动的活动的上下文。

public static void startMain (Activity context) {
   context.startActivity(new Intent (context, Page1.class));
}