0

我在我的 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");

    }

}

主布局.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>

对话框.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)
4

1 回答 1

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)in android.view.ContextThemeWrapper,这不是您的类。那是因为对话框正在使用您的布局,而不是直接使用您的活动。AFAIK,您将需要使用将侦听器setOnClickListener()Button.Dialog

于 2013-09-15T22:05:15.130 回答