Android Studio - java.lang.IllegalStateException:无法找到按钮的onClick方法

时间:2017-03-23 15:27:20

标签: java android sql database logcat

我正在尝试执行" sendMessage()"方法当" button_addData"单击按钮,但每次我尝试时,应用程序崩溃。目的是创建一个数据库并将所有输入的数据存储在其中。以下是代码片段:

package com.example.rishi.ibplanner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


import java.util.Calendar;

public class EnterDataActivity extends AppCompatActivity {

DatabaseHelper myDb;
EditText editDescription, editDuration;
Spinner spnPriority;
Long deadlineDate;
Button btnAddData;

public long sendValue() { 
    DatePicker spnDatePicker = (DatePicker) findViewById(R.id.datepicker_deadline);
    int day = spnDatePicker.getDayOfMonth();
    int month = spnDatePicker.getMonth();
    int year = spnDatePicker.getYear();

    Calendar thatDay = Calendar.getInstance();
    thatDay.set(Calendar.DAY_OF_MONTH, day);
    thatDay.set(Calendar.MONTH, month); // 0-11 so 1 less
    thatDay.set(Calendar.YEAR, year);

    Calendar today = Calendar.getInstance();

    long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
    long days = diff / (24 * 60 * 60 * 1000); //conversion into days
    return days;
}
    // Button to proceed
    public void sendMessage(View view) {



        myDb = new DatabaseHelper(this); 

        editDescription = (EditText)findViewById(R.id.edit_description);
        editDuration = (EditText)findViewById(R.id.edit_duration);
        spnPriority = (Spinner)findViewById(R.id.priority_spinner);

        deadlineDate = sendValue();

        btnAddData = (Button)findViewById(R.id.button_addData);
        addData();
}




public void addData() {
    btnAddData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                    boolean isInserted = myDb.insertData(editDescription.getText().toString(),
                            editDuration.getText().toString(),
                            spnPriority.getSelectedItem().toString(),
                            deadlineDate.toString());
                    if (isInserted = true)
                        Toast.makeText(EnterDataActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(EnterDataActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }
            }
    );
}

public void cancelActivity (View view) {
    onBackPressed();
}

}

只有按钮的xml是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rishi.ibplanner.EnterDataActivity">

<Button
    android:id="@+id/button_addData"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="12dp"
    android:onClick="sendMessage"
    android:text="@string/add" />

 </RelativeLayout>

我在logcat中收到的错误是:

java.lang.IllegalStateException: Could not find method  sendMessage[EnterDataActivity](View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_addData'
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                           at android.view.View.performClick(View.java:5637)
                                                                           at android.view.View$PerformClick.run(View.java:22429)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:0)

启动应用时会显示什么?按钮出现了吗? 因为您不会在活动中夸大XML文件,所以XML无法导航到sendMessage()方法。

您应该在活动中添加以下内容:

this.setContentView(R.layout.your_layout)