非法状态异常,应用程序无法找到类中定义的方法

时间:2013-09-15 21:32:14

标签: java android xml onclick android-alertdialog

我在我的XML文件中使用onClick设置了方法名称,但应用程序似乎无法找到它。我认为这是因为应该使用警告对话框的按钮调用该方法。任何想法将不胜感激!

主要活动:

public class MainActivity extends Activity {

    String TAG = "MainActivity";

    @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.activity_main, menu);
        return true;
    }


    public void recordExpenditure(View v) {

        Log.v(TAG, "User clicked Record Expenditure button");

        // custom dialog
        final Dialog dialog = new Dialog(v.getContext());
        dialog.setContentView(R.layout.add_expenditure_dialog_layout);
        dialog.setTitle("Add new expenditure");

        dialog.show();

    }

    public void viewExpenditure(View v) {

        Log.v(TAG, "User clicked View Expenditure button");

    }

    public void saveExpenditure(View v) {// <---- Can't find this
        DatabaseHandler db = new DatabaseHandler(this);
        // Inserting Contacts
        Log.d(TAG, "Inserting new expenditure into database");  

        Log.v(TAG, "User clicked Save Expenditure button");

        EditText saveExpenditureTitleText = (EditText)findViewById(R.id.expenditureTitleBox);
        EditText saveExpenditureValue = (EditText)findViewById(R.id.expenditureValueBox);

        String expenditureText = saveExpenditureTitleText.getText().toString();
        String expenditureValue = saveExpenditureValue.getText().toString();


        db.addExpenditure(new Expenditure(expenditureText, expenditureValue));       

        Log.v(TAG, "Recording expenditure details...\nExpenditure Title: " + expenditureText + "\nExpenditure Value: " + expenditureValue);

    }

    public void cancelExpenditure(View v) {

        Log.v(TAG, "User clicked Cancel Expenditure button");

    }

}

Main Layout.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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/recordExpenditure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:text="Add Expenditure"
        android:onClick="recordExpenditure" />

    <Button
        android:id="@+id/viewExpenditure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/recordExpenditure"
        android:layout_below="@+id/recordExpenditure"
        android:layout_marginTop="45dp"
        android:text="View Expenditure"
        android:onClick="viewExpenditure" />

</RelativeLayout>

Dialog Box.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/expenditureValueText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/expenditureTitleBox"
        android:text="Expenditure Value" />

    <TextView
        android:id="@+id/expenditureTitleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Expenditure Title" />

    <EditText
        android:id="@+id/expenditureTitleBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/expenditureTitleText"
        android:ems="10" />

    <EditText
        android:id="@+id/expenditureValueBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/expenditureValueText"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/saveExpenditureDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/expenditureValueBox"
        android:text="Save"
        android:onClick="saveExpenditure" /> <---- Can't find this

    <Button
        android:id="@+id/cancelExpenditureDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/saveExpenditureDetails"
        android:layout_alignBottom="@+id/saveExpenditureDetails"
        android:layout_alignRight="@+id/expenditureValueBox"
        android:text="Cancel"
        android:onClick="cancelExpenditure" />

</RelativeLayout>

错误:

09-15 21:12:37.411: E/AndroidRuntime(818): FATAL EXCEPTION: main
09-15 21:12:37.411: E/AndroidRuntime(818): java.lang.IllegalStateException: Could not find a method saveExpenditure(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'saveExpenditureDetails'
09-15 21:12:37.411: E/AndroidRuntime(818):  at android.view.View$1.onClick(View.java:3578)

1 个答案:

答案 0 :(得分:3)

E/AndroidRuntime(818): java.lang.IllegalStateException: Could not find a method saveExpenditure(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'saveExpenditureDetails'

您会注意到该错误消息表明Android正在saveExpenditure(View)中寻找android.view.ContextThemeWrapper,而setOnClickListener()不是您的班级。那是因为您的布局正在被对话框使用,而不是您的活动直接使用。 AFAIK,您需要使用Button将听众与Dialog中的{{1}}相关联。