2

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

这是我的 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
4

3 回答 3

1

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

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

于 2013-08-20T03:47:31.897 回答
1

在实际提取之前检查 extras 是否为空是个好主意:

if (b!= null) {
    int val = b.getInt("sec");
    Set1.setText("time: " + String.valueOf(val));
}
于 2013-08-20T03:47:37.693 回答
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);
于 2013-08-20T04:24:28.763 回答