gpt4 book ai didi

java - 请帮我调试错误 :java. lang.NullPointerException

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

我正在尝试将用户输入值从一个 Activity 传递到另一个 Activity 。在第二个 Activity 中,我对数据进行一些操作并显示最终值。但它给出了一个空指针异常。

我的代码有什么错误?

第一个 Activity :

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);

final EditText et;
final Button b;

et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v)
{
Intent intent = new Intent(Fourth.this, Fifth.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);

Intent intentnew = new Intent(Fourth.this, Sixth.class);
intentnew.putExtra("thetext", et.getText().toString());
startActivity(intentnew);

}

接收并显示数据的下一个 Activity 。(代码 fragment )

TextView tvr = (TextView) findViewById(R.id.finalremark);


double wto=Double.parseDouble(getIntent().getExtras().getString("the text"));

double z=wto*0.75;

String weight= Double.toString(z);

tvr.setText(weight);

日志猫

09-03 11:11:06.069: E/AndroidRuntime(30262): FATAL EXCEPTION: main
09-03 11:11:06.069: E/AndroidRuntime(30262): java.lang.NullPointerException
09-03 11:11:06.069: E/AndroidRuntime(30262): at com.example.footforthought.Sixth$2.handleMessage(Sixth.java:380)
09-03 11:11:06.069: E/AndroidRuntime(30262): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 11:11:06.069: E/AndroidRuntime(30262): at android.os.Looper.loop(Looper.java:137)
09-03 11:11:06.069: E/AndroidRuntime(30262): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-03 11:11:06.069: E/AndroidRuntime(30262): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 11:11:06.069: E/AndroidRuntime(30262): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 11:11:06.069: E/AndroidRuntime(30262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-03 11:11:06.069: E/AndroidRuntime(30262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-03 11:11:06.069: E/AndroidRuntime(30262): at dalvik.system.NativeStart.main(Native Method)
09-03 11:11:17.969: I/Process(30262): Sending signal. PID: 30262 SIG: 9

最佳答案

'thetext'..您在其间添加了空格..删除空格

double wto=Double.parseDouble(getIntent().getExtras().getString("thetext"));//here remove the space in name 'thetext'

double z=wto*0.75;

String weight= Double.toString(z);

tvr.setText(weight);

关于java - 请帮我调试错误 :java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637187/

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