当我单击编辑文本时.....我在logcat中得到错误
DatePickerDate.java
public class DatePickerDate extends FragmentActivity {
EditText mEdit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.DATE_EDIT_TEXT_ID);
mEdit.setText(month+"/"+day+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
}
}
search_page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectDate"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="date" />
<EditText
android:id="@+id/DATE_EDIT_TEXT_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:onClick="selectDate"
android:inputType="date" >
<requestFocus />
</EditText>
</LinearLayout>
Logcat
10-04 23:11:42.655: E/AndroidRuntime(2000): FATAL EXCEPTION: main
10-04 23:11:42.655: E/AndroidRuntime(2000): java.lang.IllegalStateException: Could not find a method selectDate(View) in the activity class com.project.findmybuffet.SearchPage for onClick handler on view class android.widget.EditText with id 'DATE_EDIT_TEXT_ID'
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.view.View$1.onClick(View.java:2131)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.view.View.performClick(View.java:2485)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.view.View$PerformClick.run(View.java:9080)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.os.Handler.handleCallback(Handler.java:587)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.os.Looper.loop(Looper.java:123)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-04 23:11:42.655: E/AndroidRuntime(2000): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 23:11:42.655: E/AndroidRuntime(2000): at java.lang.reflect.Method.invoke(Method.java:507)
10-04 23:11:42.655: E/AndroidRuntime(2000): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 23:11:42.655: E/AndroidRuntime(2000): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 23:11:42.655: E/AndroidRuntime(2000): at dalvik.system.NativeStart.main(Native Method)
10-04 23:11:42.655: E/AndroidRuntime(2000): Caused by: java.lang.NoSuchMethodException: selectDate
10-04 23:11:42.655: E/AndroidRuntime(2000): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
10-04 23:11:42.655: E/AndroidRuntime(2000): at java.lang.Class.getMethod(Class.java:962)
10-04 23:11:42.655: E/AndroidRuntime(2000): at android.view.View$1.onClick(View.java:2124)
如何解决这个问题!
[更新]
SearchPage.java
公共类SearchPage扩展Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_page);
}
}
最佳答案
因为SearchPage
也使用您的search_page
布局,所以它还需要一个selectDate(View)
方法。
单击带有android:onClick="selectDate"
的视图时,Android将在当前显示给用户的活动上调用selectDate(View)
。
在您的情况下,显示的是SearchPage
活动,由于Android在该活动中找不到selectDate(View)
,因此应用程序崩溃。因此,logcat的第一行,
在活动类com.project.findmybuffet.SearchPage中找不到方法selectDate(View)