我尝试运行我的应用程序后出现此运行时错误,这是一个带有5个按钮和2个EditText和1个TextView的简单计算器。
MainActivity.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button sum,sub,mult,div,clear;
TextView tv;
EditText e,e1;
float num1,num2,result;
String oper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sum=(Button)findViewById(R.id.button_sum);
sub=(Button)findViewById(R.id.button_sub);
mult=(Button)findViewById(R.id.button_mul);
clear=(Button)findViewById(R.id.button_clear);
sum=(Button)findViewById(R.id.button_sum);
e=(EditText)findViewById(R.id.editText);
e1=(EditText)findViewById(R.id.editText2);
tv=(TextView)findViewById(R.id.textView);
sum.setOnClickListener(this);
sub.setOnClickListener(this);
mult.setOnClickListener(this);
div.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
num1 = Float.parseFloat(e.getText().toString());
num2 = Float.parseFloat(e1.getText().toString());
if(TextUtils.isEmpty(e.getText().toString())&&TextUtils.isEmpty(e1.getText().toString()))
{
tv.setText("Error");
return;
}
switch (v.getId()){
case R.id.button_sum:
oper="+";
result=num1+num2;
break;
case R.id.button_sub:
oper="-";
result=num1-num2;
break;
case R.id.button_mul:
oper="*";
result=num1*num2;
break;
case R.id.button_div:
oper="/";
result=num1/num2;
break;
case R.id.button_clear:
tv.setText("");
e.setText("");
e1.setText("");
break;
default:
break;
}
tv.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
xml layout:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button_sum"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/button_sub"
android:layout_alignTop="@+id/button_sum"
android:layout_toRightOf="@+id/button_sum"
android:layout_toEndOf="@+id/button_sum" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/button_mul"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/button_sub"
android:layout_toEndOf="@+id/button_sub" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/button_div"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/button_mul"
android:layout_toEndOf="@+id/button_mul" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginTop="64dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignRight="@+id/button_sub"
android:layout_alignEnd="@+id/button_sub"
android:textSize="20sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_toRightOf="@+id/editText"
android:layout_toEndOf="@+id/editText"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="30sp"
android:layout_below="@+id/button_sub"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear"
android:id="@+id/button_clear"
android:layout_below="@+id/button_sub"
android:layout_toRightOf="@+id/button_sum"
android:layout_toEndOf="@+id/button_sum" />
logcat:
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: FATAL EXCEPTION: main
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: Process: com.example.megam.myapplication, PID: 1798
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.megam.myapplication/com.example.megam.myapplication.MainActivity}: java.lang.NullPointerException
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at com.example.megam.myapplication.MainActivity.onCreate(MainActivity.java:32)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5231)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-29 05:04:47.493 1798-1798/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-29 05:04:47.501 1798-1800/? D/dalvikvm: GC_CONCURRENT freed 212K, 8% free 3199K/3464K, paused 5ms+1ms, total 11ms
11-29 05:04:49.337 1798-1798/? I/Process: Sending signal. PID: 1798 SIG: 9
在我添加清除按钮之前,它曾经工作过。