-8

我的应用程序 android 崩溃了。
日志错误:

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);
4

3 回答 3

5

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

检查你在那里写的。

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

验证您在其中有值,您也可以调试。

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

于 2012-07-09T12:14:07.633 回答
0

您的日期对象在第 111 行为空,因为您的意图可能没有名为日期的键,否则该值将为空。

为防止出现此异常:

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

前任:

if (date != null)
     labelDate.setText(date);
于 2012-07-09T12:28:31.327 回答
0

看看这段代码:

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

希望这会帮助你。

于 2012-07-09T12:33:20.840 回答