gpt4 book ai didi

java - 将 double 从一项 Activity 转移到另一项 Activity

转载 作者:行者123 更新时间:2023-12-02 10:26:03 25 4
gpt4 key购买 nike

我正在尝试将通过 EditText 获得的变量传输到下一个 Activity 中。起初,我只用一个变量尝试过,效果很好。但是一旦我添加了第二个 Activity ,应用程序就开始崩溃一旦我尝试过。当我尝试按下按钮进入第二个 Activity 时,应用程序崩溃了。也许你能找到错误?

以下是我的代码。

第一个 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button weiter = (Button) findViewById(R.id.weiter1);
weiter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText inputHeight = (EditText) findViewById(R.id.inputheight);
EditText inputlenght = (EditText) findViewById(R.id.inputlenght);

Double height = Double.parseDouble(inputHeight.getText().toString());
String Höhe = new Double(height).toString();

Double lenght = Double.parseDouble(inputlenght.getText().toString());
String Länge = new Double(lenght).toString();

Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("HöheName", Höhe);
i.putExtra( "LängeName", Länge);
startActivity(i);
}
});
}

第二个 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String Höhe = intent.getExtras().getString("HöheName");
String Länge = intent.getExtras().getString( "längeName");

double höhe = Double.valueOf(Höhe);
double länge = Double.valueOf(Länge);

double längemalhöhe = höhe + länge;

String ergebnis = new Double(längemalhöhe).toString();

TextView Test = (TextView) findViewById(R.id.Test);

Test.setText(ergebnis);

Button weiter = (Button) findViewById(R.id.weiter2);
weiter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = getIntent();
String Höhe = intent.getExtras().getString("HöheName");

Intent i = new Intent(getApplicationContext(), Main3Activity.class);
i.putExtra("HöheName", Höhe);
startActivity(i);
}
});
}

第三个 Activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);

Intent intent = getIntent();
String Höhe = intent.getExtras().getString("HöheName");

TextView Test = (TextView) findViewById(R.id.Ergebnis2);

Test.setText(Höhe);
}

这是 Logcat:

12-27 23:02:42.760 30997-30997/com.example.june.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.june.test, PID: 30997
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.june.test/com.example.june.test.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
at java.lang.StringToReal.parseDouble(StringToReal.java:263)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at com.example.june.test.Main2Activity.onCreate(Main2Activity.java:22)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7331) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

我不知道这是否只是一个拼写错误,但你有这样一行:String Länge =intent.getExtras().getString(“längeName”);其中“längeName” 不是大写,但是,您最初是这样做的:i.putExtra( "LängeName", Länge); 其中 "LängeName" 是大写。所以问题可能是两个字符串不相同。当您尝试执行以下操作时:

String Länge = intent.getExtras().getString( "längeName");

字符串被初始化为 null,因为没有值分配给 "längeName",如果尝试使用字符串。

关于java - 将 double 从一项 Activity 转移到另一项 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53951377/

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