qq_21397217
Arvin ZHANG
采纳率66.7%
2017-03-14 02:34 阅读 3.2k

切换Activity时报错Unable to stop activity

请教一下大家,哪些原因可能导致下面出现的问题!

程序有两个Activity,从MainActivity启动到NoteActivity时报错,主要是说java.lang.RuntimeException: Unable to stop activity {<包名>/<包名>.MainActivity}: java.lang.NullPointerExceptionCaused by: java.lang.NullPointerException,日志里没有说我的代码哪行有问题,都是些系统自己的类和方法。

两个Activity的声明:

<activity
    android:name=".activity.MainActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".activity.NoteActivity"
    android:theme="@style/AppTheme.NoActionBar" />

启动NoteActivity的代码:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new_note);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, NoteActivity.class);
        intent.putExtra("Open_Type", "newNote");
        startActivity(intent);
    }
});

点了fab按钮后直接报错,完整的错误日志如下:

E/AndroidRuntime: FATAL EXCEPTION: main
  Process: zqy.bysj.todo, PID: 2034
  java.lang.RuntimeException: Unable to stop activity {zqy.bysj.todo/zqy.bysj.todo.activity.MainActivity}: java.lang.NullPointerException
      at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3174)
      at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3220)
      at android.app.ActivityThread.access$1100(ActivityThread.java:135)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:136)
      at android.app.ActivityThread.main(ActivityThread.java:5001)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
      at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
      at android.app.Activity.performStop(Activity.java:5388)
      at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3171)
      at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3220) 
      at android.app.ActivityThread.access$1100(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5001) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
      at dalvik.system.NativeStart.main(Native Method) 

请教一下大家,哪些原因可能导致这个问题?谢谢!

附图,看的清楚点。
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • as_csdn 代码的灵魂是bug! 2017-03-14 06:27

    只知道提示的是空指针错误,将完整代码贴出来看看

    点赞 评论 复制链接分享
  • sadcui sadcui 2017-03-15 01:52

    你不会是在NoteActivity里面finish MainActivity了?

    点赞 评论 复制链接分享
  • jinjin10086 GJianAJ 2017-03-15 09:01

    你的MainActivity可能其他操作影响了

    点赞 评论 复制链接分享

相关推荐