如何在Android Studio中修复“ IllegalStateException”(致命异常)?

时间:2019-07-16 05:18:23

标签: java android

我是Android Studio的新手,正在尝试构建一个执行加法运算的计算器。在手机上执行该应用程序时,出现错误消息“ IllegalStateException”,并且手机上显示一条消息,表明该应用程序已停止工作。请帮忙。

public class MainActivity extends AppCompatActivity {

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

    }
    public void onButtonClick(View v) throws IllegalStateException
    {
        EditText e1 = (EditText)findViewById(R.id.editText1);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.view);
        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());
        int RESULT = num1+num2;
        t1.setText(Integer.toString(RESULT));

    }
}

输入值后运行后的堆栈跟踪

--------- beginning of crash

2019-07-16 11:02:03.122 14400-14400 / com.example.calculator E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.calculator,PID:14400     java.lang.IllegalStateException:无法执行android:onClick的方法         在androidx.appcompat.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)         在android.view.View.performClick(View.java:5675)         在android.view.View $ PerformClick.run(View.java:22646)         在android.os.Handler.handleCallback(Handler.java:836)         在android.os.Handler.dispatchMessage(Handler.java:103)         在android.os.Looper.loop(Looper.java:203)         在android.app.ActivityThread.main(ActivityThread.java:6251)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1075)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)      造成原因:java.lang.reflect.InvocationTargetException         在java.lang.reflect.Method.invoke(本机方法)         在androidx.appcompat.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)         在android.view.View.performClick(View.java:5675)         在android.view.View $ PerformClick.run(View.java:22646)         在android.os.Handler.handleCallback(Handler.java:836)         在android.os.Handler.dispatchMessage(Handler.java:103)         在android.os.Looper.loop(Looper.java:203)         在android.app.ActivityThread.main(ActivityThread.java:6251)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1075)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)      造成原因:java.lang.ClassCastException:android.view.View无法转换为android.widget.TextView         在com.example.calculator.MainActivity.onButtonClick(MainActivity.java:22)         在java.lang.reflect.Method.invoke(本机方法)         在androidx.appcompat.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)         在android.view.View.performClick(View.java:5675)         在android.view.View $ PerformClick.run(View.java:22646)         在android.os.Handler.handleCallback(Handler.java:836)         在android.os.Handler.dispatchMessage(Handler.java:103)         在android.os.Looper.loop(Looper.java:203)         在android.app.ActivityThread.main(ActivityThread.java:6251)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1075)         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

2 个答案:

答案 0 :(得分:0)

尝试一下:

public class MainActivity extends AppCompatActivity {

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

}

public void onButtonClick(View view){
    EditText e1 = view.findViewById(R.id.editText1);
    EditText e2 = view.findViewById(R.id.editText2);
    TextView t1 = view.findViewById(R.id.view);
    int num1 = Integer.parseInt(e1.getText().toString());
    int num2 = Integer.parseInt(e2.getText().toString());
    int result = num1+num2;
    t1.setText(Integer.toString(result));
}   

}

答案 1 :(得分:0)

这是错误:

  

java.lang.ClassCastException:android.view.View无法转换为   android.widget.TextView位于   com.example.calculator.MainActivity.onButtonClick(MainActivity.java:22)

在此行:

TextView t1 = (TextView)findViewById(R.id.view);

因为R.id.viewView的ID,而不是TextView的ID(请检查布局xml)。

您需要在布局文件中使该视图成为textview。