在Android中获取捆绑值

时间:2013-08-19 17:48:29

标签: java android bundle

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

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

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

logcat的:

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

2 个答案:

答案 0 :(得分:0)

您需要调试代码并在断点处设置断点,以便检查捆绑中的值。如果不这样做,你的猜测就和我们的一样好。

答案 1 :(得分:0)

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

  

向intent添加一组扩展数据。 密钥必须包含包前缀,例如app com.android.contacts将使用“com.android.contacts.ShowAll”这样的名称。

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