在按钮上启动级联活动单击

时间:2014-02-04 21:39:26

标签: java android

我在Android应用程序中有各种活动,可以说Activity1,Activity2,Activity3等

全部由按钮上的intent对象启动单击或列出视图选择事件。在Activity1中的某些活动中,我将一个包存储在一个intent对象中,然后启动一个Activity2,在第二个Activity上,startActivity()方法不起作用是与内存相关的问题,如意图太重,无法进入下一个Activity3。

以下是我的代码

Class Activity2 extends Activity
{
     private Intent i;
     private Button btn;
     @Override 
      protected void onCreate(Bundle b)
      {
            //super...
            i=new Intent(Activity2.this,Activity3.class);
            //btn=.....

//以下是按钮点击事件,我将从中导航到Activity3

btn.setOnClickListener(new OnClickListener() 
        {    

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        startActivity(i); //here it crashed
    }});

}

是内存相关问题还是编码问题?如果编码问题,那么除了意图之外,还可以启动Activity。

以下是Log

02-05 02:55:53.367: E/AndroidRuntime(4500): FATAL EXCEPTION: main
02-05 02:55:53.367: E/AndroidRuntime(4500): java.lang.RuntimeException: Unable to pause activity {com.example.viva_project/com.example.viva_project.MainActivity}: java.lang.NullPointerException
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.access$1700(ActivityThread.java:117)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.os.Looper.loop(Looper.java:130)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.main(ActivityThread.java:3687)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at java.lang.reflect.Method.invokeNative(Native Method)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at java.lang.reflect.Method.invoke(Method.java:507)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at dalvik.system.NativeStart.main(Native Method)
02-05 02:55:53.367: E/AndroidRuntime(4500): Caused by: java.lang.NullPointerException
02-05 02:55:53.367: E/AndroidRuntime(4500):     at com.example.viva_project.MainActivity.onPause(MainActivity.java:309)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.Activity.performPause(Activity.java:3851)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
02-05 02:55:53.367: E/AndroidRuntime(4500):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
02-05 02:55:53.367: E/AndroidRuntime(4500):     ... 12 more



protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    connectThread.killConnectThread();
     }

2 个答案:

答案 0 :(得分:0)

你必须在你的清单中提到所有3个活动。

答案 1 :(得分:0)

Caused by: java.lang.NullPointerException at com.example.viva_project.MainActivity.onPause(MainActivity.java:309)

问题在于:

connectThread.killConnectThread();

connectThreadnull,您正在尝试执行方法killConnectThread()

请务必在您的Manifest.xml

中注册Activity1,Activity2和Activity3