我试图将我的主要活动中的值传递给我的完成活动,但我的完成活动总是出错。我不知道我的代码有什么问题。
这是我的 Level1 课程的代码:
注意: Chronometer 已经在我的活动顶部定义,在 onCreate 方法之前。
private void showElapsedTime() {
chrono.start();
long timeElapsed = SystemClock.elapsedRealtime() - chrono.getBase();
int hours = (int) (timeElapsed / 3600000);
int minutes = (int) (timeElapsed - hours * 3600000) / 60000;
int seconds = (int) (timeElapsed - hours * 3600000 - minutes * 60000) / 1000;
int seco = seconds;
Toast.makeText(this, "Elapsed milliseconds: " + seconds,
Toast.LENGTH_SHORT).show();
Intent a = new Intent (this, Finish.class);
Bundle b = new Bundle();
b.putInt("sec", seco);
a.putExtras(b);
}
这是我的 Finish.class 代码:
Set1 = (TextView) findViewById (R.id.time1);
Bundle b = getIntent().getExtras();
Set1.setText("time: " + b.getInt("seco"));
日志猫:
08-20 11:37:49.345: E/AndroidRuntime(815): FATAL EXCEPTION: main
08-20 11:37:49.345: E/AndroidRuntime(815): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathattack/com.example.mathattack.Finish}: java.lang.NullPointerException
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.os.Looper.loop(Looper.java:123)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-20 11:37:49.345: E/AndroidRuntime(815): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 11:37:49.345: E/AndroidRuntime(815): at java.lang.reflect.Method.invoke(Method.java:521)
08-20 11:37:49.345: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-20 11:37:49.345: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-20 11:37:49.345: E/AndroidRuntime(815): at dalvik.system.NativeStart.main(Native Method)
08-20 11:37:49.345: E/AndroidRuntime(815): Caused by: java.lang.NullPointerException
08-20 11:37:49.345: E/AndroidRuntime(815): at com.example.mathattack.Finish.onCreate(Finish.java:33)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-20 11:37:49.345: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-20 11:37:49.345: E/AndroidRuntime(815): ... 11 more