0

我仍然很困惑为什么我从主要活动的捆绑包中获取值时出错。

所以,这是我的代码片段:

Level1.class

Chronometer chrono;
chrono = (Chronometer)findViewById(R.id.chronometer1);
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;

Intent lvl1 = new Intent(getApplicationContext(), Finish.class);    
Bundle time1 = new Bundle();
time1.putInt("hour1", hours);
time1.putInt("minutes1", minutes);
time1.putInt("seconds1", seconds);

lvl1.putExtras(time1);

和我的Finish.class:(在 onCreate 方法内)

TextView Set1;

Set1 = (TextView) findViewById (R.id.time1);

Bundle time1 = getIntent().getExtras();
int hrs = time1.getInt("hour1");
int min = time1.getInt("minutes1"); 
int sec = time1.getInt("seconds1");


Set1.setText(hrs + ":" + min + ":" + sec);

谁能告诉我为什么我有错误?

注意:第 30 行中的错误,即int hrs = time1.getInt("hour1");.

日志猫:

08-20 01:54:10.461: E/AndroidRuntime(950): FATAL EXCEPTION: main
08-20 01:54:10.461: E/AndroidRuntime(950): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathattack/com.example.mathattack.Finish}: java.lang.NullPointerException
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.os.Looper.loop(Looper.java:123)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-20 01:54:10.461: E/AndroidRuntime(950):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 01:54:10.461: E/AndroidRuntime(950):  at java.lang.reflect.Method.invoke(Method.java:521)
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-20 01:54:10.461: E/AndroidRuntime(950):  at dalvik.system.NativeStart.main(Native Method)
08-20 01:54:10.461: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
08-20 01:54:10.461: E/AndroidRuntime(950):  at com.example.mathattack.Finish.onCreate(Finish.java:30)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-20 01:54:10.461: E/AndroidRuntime(950):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-20 01:54:10.461: E/AndroidRuntime(950):  ... 11 more
4

2 回答 2

0

您需要调试代码并在从意图中获取捆绑包的位置放置断点,以检查捆绑包中的值。如果不这样做,您的猜测与我们的猜测一样好。

于 2013-08-19T17:52:50.500 回答
0

来自putExtras(强调我的)的文档:

向意图添加一组扩展数据。键必须包含包前缀,例如应用程序 com.android.contacts 将使用类似“com.android.contacts.ShowAll”的名称。

改为使用Intent.putExtra("hour1", hours);

于 2013-08-19T17:55:47.217 回答