gpt4 book ai didi

java - 当我单击按钮打开此 Activity 时,它会关闭

转载 作者:行者123 更新时间:2023-12-01 13:10:32 25 4
gpt4 key购买 nike

主要 Activity 有用于打开各种事物的新 Activity 的按钮。当我单击按钮时,一项 Activity (计算器)就会关闭。其他 Activity 运作良好。

该 Activity 的 xml 文件有问题:

<AbsoluteLayout android:id="@+id/widget0" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADDITION OF NUMBERS"
android:id="@+id/textView1"
android:layout_x="73dip"
android:layout_y="28dip"></TextView>

<TextView android:layout_width="wrap_content"
android:layout_x="36dip"
android:layout_height="wrap_content"
android:text="First Amount"
android:id="@+id/textView2"
android:layout_y="80dip"></TextView>

<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/amount1"
android:layout_height="wrap_content"
android:layout_y="62dip"></EditText>

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Amount"
android:id="@+id/textView3"
android:layout_x="36dip"
android:layout_y="169dip"></TextView>

<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="169dip"
android:id="@+id/amount2"
android:layout_height="wrap_content"
android:layout_y="152dip"></EditText>

<Button android:layout_width="wrap_content"
android:id="@+id/calculate"
android:layout_x="41dip"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_y="232dip"></Button>

<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/tt"
android:layout_height="wrap_content"
android:layout_y="232dip"></EditText>

</AbsoluteLayout>

java 文件是:

package com.example.welderhelper;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;


public class Calculators extends ActionBarActivity
{
EditText amount1;
EditText amount2;
TextView tt;
Button calculate;
double x=0;
double y=0;
double z=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculators);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
amount2=(EditText)findViewById(R.id.amount2);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=x+y;
tt.setText(Double.toString(z));
}
}

我试图用作引用,http://www.techillumination.in/2010/02/simple-android-application-for-adding.html ,但没有成功。因此,即使我复制并粘贴它,更改 Activity 名称,它仍然不起作用。我正在学习,所以我迷失了。它只是假设取 2 个数字并将它们加在一起。感谢您的帮助。

编辑:这是 logcat:

04-06 19:35:50.286: V/Activity(4144): onPause  com.example.welderhelper.MainActivity@4204ef40
04-06 19:35:50.306: V/Activity(4144): onCreate com.example.welderhelper.Calculators@42346760: null
04-06 19:35:50.336: D/AndroidRuntime(4144): Shutting down VM
04-06 19:35:50.336: W/dalvikvm(4144): threadid=1: thread exiting with uncaught exception (group=0x41dd0700)
04-06 19:35:50.336: E/AndroidRuntime(4144): FATAL EXCEPTION: main
04-06 19:35:50.336: E/AndroidRuntime(4144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.welderhelper/com.example.welderhelper.Calculators}: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.os.Looper.loop(Looper.java:137)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.main(ActivityThread.java:5290)
04-06 19:35:50.336: E/AndroidRuntime(4144): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 19:35:50.336: E/AndroidRuntime(4144): at java.lang.reflect.Method.invoke(Method.java:525)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-06 19:35:50.336: E/AndroidRuntime(4144): at dalvik.system.NativeStart.main(Native Method)
04-06 19:35:50.336: E/AndroidRuntime(4144): Caused by: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.initControls(Calculators.java:34)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.onCreate(Calculators.java:26)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.Activity.performCreate(Activity.java:5243)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
04-06 19:35:50.336: E/AndroidRuntime(4144): ... 11 more
04-06 19:35:51.527: I/Process(4144): Sending signal. PID: 4144 SIG: 9

最佳答案

如果您的意思是您的应用程序在按下标记为“计算”的按钮后崩溃,则可能是因为其中一个 parseDouble() 方法调用引发了 NumberFormatException。像这样更改您的 calculate() 方法:

private void calculate()
{
try
{
x = Double.parseDouble(amount1.getText().toString());
y = Double.parseDouble(amount2.getText().toString());
z = x + y;
tt.setText(Double.toString(z));
}
catch (NumberFormatException e)
{
tt.setText("Invalid input");
}
}

关于java - 当我单击按钮打开此 Activity 时,它会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22901743/

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