无法在两个活动之间传递int值

时间:2013-08-20 03:44:21

标签: java android int bundle

我试图将值从我的主要活动传递到我的完成活动,但我的完成活动总是出错。我不知道我的代码有什么问题。

这是我的Level1类的代码:

注意:在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"));

logcat的:

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

3 个答案:

答案 0 :(得分:1)

您的int以“sec”名称存储。尝试使用下面的行来检索它。

Set1.setText("time: " + String.valueOf(b.getInt("sec")));

答案 1 :(得分:1)

最好在实际提取之前检查extras是否为null:

if (b!= null) {
    int val = b.getInt("sec");
    Set1.setText("time: " + String.valueOf(val));
}

答案 2 :(得分:0)

您也可以尝试使用一个对象,您可以在oncreate方法之前将要使用的变量放在其他活动中,这样我也可以被其他类使用。

public class ... extends Activity { 

     int p; // int which you want to share.

 @Override

 protected void onCreate(Bundle savedInstanceState){
       ........
       }
}

,在下一课程中,您可以使用以下方法:

MainActivity mn = new MainActivity ();

System.out.println(mn.p);