在另一个类中启动另一个活动

时间:2017-01-17 08:53:39

标签: java php android

我有三个类:MainActivity,BackgroundActivity和roomActivity BackgroundActivity将收到php服务器反馈。 如果没有登录失败,它将调用RoomActivity类 我想知道为什么不起作用?


BackgroundActivity:

 protected void onPostExecute(String result) {
            if (result.equals("<meta charset=\"utf-8\">login fail")) {
                alertDialog.setMessage("Please check your login email");
                alertDialog.show();
            } else {
                **Intent myIntent = new Intent(MainActivity.class, RoomActivity.class);
                startActivity(myIntent);**
    }





E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.a20_1discussboard, PID: 2262
                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                      at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
                      at android.content.ComponentName.<init>(ComponentName.java:77)
                      at android.content.Intent.<init>(Intent.java:4160)
                      at com.example.a20_1discussboard.MainActivity.check(MainActivity.java:38)
                      at com.example.a20_1discussboard.BackgroundWorker.onPostExecute(BackgroundWorker.java:151)
                      at com.example.a20_1discussboard.BackgroundWorker.onPostExecute(BackgroundWorker.java:24)
                      at android.os.AsyncTask.finish(AsyncTask.java:636)
                      at android.os.AsyncTask.access$500(AsyncTask.java:177)
                      at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

3 个答案:

答案 0 :(得分:1)

将其更改为
Intent myIntent = new Intent(MainActivity.this, RoomActivity.class);

答案 1 :(得分:1)

从活动外部拨打startActivity()可能会产生AndroidRuntimeException,使用Context就像:

Intent myIntent = new Intent(MainActivity.class, RoomActivity.class);
getApplicationContext().startActivity(myIntent);

答案 2 :(得分:0)

意图构造函数错误。

应该是这样的:

Intent myIntent = new Intent(getApplicationContext(), RoomActivity.class);
startActivity(myIntent);

您可以发布有关该问题的更多信息吗?