应用程序崩溃。 RuntimeException:无法启动活动

时间:2012-07-09 12:11:47

标签: android eclipse nullpointerexception logcat

我的应用程序android崩溃了 logcat错误:

07-09 12:00:46.486: D/dalvikvm(249): GC freed 613 objects / 50984 bytes in 89ms
07-09 12:00:49.944: D/dalvikvm(249): GC freed 2700 objects / 118760 bytes in 76ms
07-09 12:00:50.406: D/AndroidRuntime(249): Shutting down VM
07-09 12:00:50.406: W/dalvikvm(249): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-09 12:00:50.417: E/AndroidRuntime(249): Uncaught handler: thread main exiting due to uncaught exception
07-09 12:00:50.444: E/AndroidRuntime(249): java.lang.RuntimeException: Unable to start activity ComponentInfo{allo.nascubetest/allo.nascubetest.MainActivity2}: java.lang.NullPointerException
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.os.Looper.loop(Looper.java:123)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-09 12:00:50.444: E/AndroidRuntime(249):  at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:00:50.444: E/AndroidRuntime(249):  at java.lang.reflect.Method.invoke(Method.java:521)
07-09 12:00:50.444: E/AndroidRuntime(249):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 12:00:50.444: E/AndroidRuntime(249):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 12:00:50.444: E/AndroidRuntime(249):  at dalvik.system.NativeStart.main(Native Method)
07-09 12:00:50.444: E/AndroidRuntime(249): Caused by: java.lang.NullPointerException
07-09 12:00:50.444: E/AndroidRuntime(249):  at allo.nascubetest.MainActivity2.onCreate(MainActivity2.java:111)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 12:00:50.444: E/AndroidRuntime(249):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-09 12:00:50.444: E/AndroidRuntime(249):  ... 11 more
07-09 12:00:50.454: I/dalvikvm(249): threadid=7: reacting to signal 3
07-09 12:00:50.554: I/dalvikvm(249): Wrote stack trace to '/data/anr/traces.txt'
07-09 12:00:52.084: I/Process(249): Sending signal. PID: 249 SIG: 9

在我的OnCreate中:

    Intent thisIntent = getIntent();
    date = thisIntent.getExtras().getString("date");

和我的第111行:

    labelDate.setText(date);

3 个答案:

答案 0 :(得分:5)

您正在访问文件MainActivity2.java中第111行的空变量。

检查你在那里写的是什么。

从您的代码看,似乎labelDate为null或date为null。

确认您有值,也可以调试。

一般来说,最好从developer.android.com做一些教程。

答案 1 :(得分:0)

你的日期对象在第111行是空的,b'coz你的意图可能没有命名为date的键,或者该值将为null。

要防止此例外:

  1. 确保labelDate不为空
  2. 在分配给setText();
  3. 之前,确保日期对象不为null

    前:

    if (date != null)
         labelDate.setText(date);
    

答案 2 :(得分:0)

查看此代码:

Bundle bundle=getIntent().getExtras();
if(bundle!=null){
String date=bundle.getString("date");
labelDate.setText(date);
}

希望这会对你有所帮助。