错误`java.lang.IllegalStateException:无法执行活动的方法`

时间:2015-09-16 18:53:58

标签: java android

我刚刚开始安装android编程,我想制作一个sorta计算器。

package com.example.gourab.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void putDigits(View view)
    {
        TextView display = (TextView) findViewById(R.id.display);
        String prev = ((TextView) findViewById(R.id.display)).getText().toString();
        int prevint = Integer.parseInt(prev);
        switch (view.getId())
        {
            case R.id.num1:
                display.setText(prevint * 10 + 1);
                break;
            case R.id.num2:
                display.setText(prevint * 10 + 2);
                break;
        }
    }
}

我刚刚在交换机中使用了两个用于测试的案例!

这是我的activity_main.xml

<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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/display"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLines="1"
        android:height="30dp"
        android:text="0"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/num1"
        android:layout_marginTop="150dp"
        android:layout_below="@+id/display"
        android:layout_toLeftOf="@+id/num2"
        android:layout_toStartOf="@+id/num2"
        android:layout_marginRight="37dp"
        android:layout_marginEnd="37dp"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:id="@+id/num2"
        android:layout_alignTop="@+id/num1"
        android:layout_toLeftOf="@+id/num3"
        android:layout_toStartOf="@+id/num3"
        android:layout_marginRight="38dp"
        android:layout_marginEnd="38dp"
        android:textColor="#f00"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/num3"
        android:layout_alignTop="@+id/num2"
        android:layout_alignRight="@+id/display"
        android:layout_alignEnd="@+id/display"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        android:id="@+id/num4"
        android:layout_marginTop="27dp"
        android:layout_below="@+id/num1"
        android:layout_alignLeft="@+id/num1"
        android:layout_alignStart="@+id/num1"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/num5"
        android:layout_alignBottom="@+id/num4"
        android:layout_toRightOf="@+id/num1"
        android:layout_toEndOf="@+id/num1"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:id="@+id/num6"
        android:layout_alignTop="@+id/num5"
        android:layout_alignLeft="@+id/num3"
        android:layout_alignStart="@+id/num3"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        android:id="@+id/num7"
        android:layout_marginTop="29dp"
        android:layout_below="@+id/num4"
        android:layout_alignLeft="@+id/num4"
        android:layout_alignStart="@+id/num4"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:id="@+id/num8"
        android:layout_alignTop="@+id/num7"
        android:layout_alignLeft="@+id/num5"
        android:layout_alignStart="@+id/num5"
        android:onClick="putDigits" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/num9"
        android:layout_alignTop="@+id/num8"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="putDigits" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="×"
        android:id="@+id/multiply"
        android:layout_alignTop="@+id/num1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="÷"
        android:id="@+id/divide"
        android:layout_alignTop="@+id/num4"
        android:layout_alignRight="@+id/multiply"
        android:layout_alignEnd="@+id/multiply" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/add"
        android:layout_alignBottom="@+id/num7"
        android:layout_alignRight="@+id/divide"
        android:layout_alignEnd="@+id/divide" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/equal"
        android:layout_below="@+id/num8"
        android:layout_alignRight="@+id/num9"
        android:layout_alignEnd="@+id/num9" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/subtract"
        android:layout_below="@+id/num7"
        android:layout_alignLeft="@+id/add"
        android:layout_alignStart="@+id/add" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:id="@+id/num0"
        android:layout_alignBottom="@+id/equal"
        android:layout_alignLeft="@+id/num7"
        android:layout_alignStart="@+id/num7" />

</RelativeLayout>

它给了我这个错误 -

09-17 00:14:22.932    1643-1643/com.example.gourab.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.gourab.test, PID: 1643
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4025)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
            at android.content.res.Resources.getText(Resources.java:299)
            at android.widget.TextView.setText(TextView.java:4138)
            at com.example.gourab.test.MainActivity.putDigits(MainActivity.java:48)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

我不知道是什么原因引起的。但任何帮助都会非常感激。

谢谢!

0 个答案:

没有答案