1
我試圖在單擊「button_addData」按鈕時執行「sendMessage()」方法,但每次嘗試時,應用程序都會崩潰。目的是創建一個數據庫並將所有輸入的數據存儲在其中。下面是代碼片段:Android Studio - java.lang.IllegalStateException:無法找到按鈕的onClick方法
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)
能告訴那裏的OnCreate代碼編寫的代碼片段,也沒有必要添加該按鈕聽衆,你已經通過XML指定它onclick屬性所以從addData中刪除監聽器 –