我试图将特定的int变量传递给另一个类,但它使程序崩溃。请看一看:

第一次活动

public void highQuiz(View view){

    Intent j= new Intent(this,HighScoreTable.class);

    j.putExtra("new_variable_name",(int)lowerInt);


    startActivityForResult(j, 0);   }


第二次活动

    TextView hs1 = (TextView)findViewById(R.id.textView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int lowerInt= extras.getInt("new_variable_name");
        hs1.setText(""+lowerInt);
    }


日志记录

04-29 18:36:21.243: E/AndroidRuntime(2104): FATAL EXCEPTION: main

04-29 18:36:21.243: E/AndroidRuntime(2104): Process: oxley.It, PID: 2104

04-29 18:36:21.243: E/AndroidRuntime(2104): java.lang.RuntimeException: Unable to start activity ComponentInfo{oxley.It/oxley.It.HighScoreTable}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread.access$800(ActivityThread.java:144)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.os.Handler.dispatchMessage(Handler.java:102)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.os.Looper.loop(Looper.java:135)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread.main(ActivityThread.java:5221)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at java.lang.reflect.Method.invoke(Native Method)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at java.lang.reflect.Method.invoke(Method.java:372)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

04-29 18:36:21.243: E/AndroidRuntime(2104): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

04-29 18:36:21.243: E/AndroidRuntime(2104):     at oxley.It.HighScoreTable.onCreate(HighScoreTable.java:25)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.Activity.performCreate(Activity.java:5933)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)

04-29 18:36:21.243: E/AndroidRuntime(2104):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

04-29 18:36:21.243: E/AndroidRuntime(2104):     ... 10 more

最佳答案

尝试

         if(getIntent().hasExtra("new_variable_name"))
                  getIntent().getIntExtra("new_variable_name");

09-05 04:18