主要活动包含用于打开各种事物的新活动的按钮。单击按钮后,一个活动(计算器)就会关闭。其他活动工作正常。
活动的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,但它没有用。因此,即使我复制并粘贴它,更改活动名称,它仍然无法正常工作。我正在学习所以我迷失了。它只是假设取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
答案 0 :(得分:1)
如果您的意思是在按下标记为计算的按钮后应用程序崩溃,可能是因为其中一个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");
}
}
答案 1 :(得分:0)
从logcat
中的这两行开始Caused by: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.initControls(Calculators.java:34)
null
第34行的某些内容initControls()
似乎是此行
calculate.setOnClickListener(new Button.OnClickListener()
这意味着您Button
activity_calculators.xml
id
calculate
Activity
。您发布的xml似乎是您的第一个id
而不是您发布的那个,因为您发布的xml中不存在java文件中的id
个。您尝试引用的activity_calculators.xml
应位于id
。
修改强>
好的,我真的需要多加注意。那些activity_calculators.xml
位于您发布的xml中,因此要么不是{{1}},要么需要清理和重建项目。
答案 2 :(得分:0)
尝试更改
calculate.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
calculate();
}
});
到
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
calculate();
}
});