gpt4 book ai didi

java - 使用 putExtras 将 int 变量传递给另一个 Activity 会导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 11:36:27 26 4
gpt4 key购买 nike

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

第一个 Activity

public void highQuiz(View view){

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

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


startActivityForResult(j, 0); }

第二个 Activity

    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

最佳答案

从堆栈跟踪看来 textview 为空

    TextView hs1 = (TextView)findViewById(R.id.textView1);

确保你已经定义了textview并且在findViewById之后它不为空。

关于java - 使用 putExtras 将 int 变量传递给另一个 Activity 会导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com