Android contextMenu上的NoSuchMethodException

时间:2013-05-09 07:22:27

标签: android contextmenu android-dialogfragment

我的应用程序有一个按钮,单击该按钮可显示上下文菜单。单击第一个itema时,将打开一个datepicker对话框片段。

它在Android 2.3中运行,但我在Android 4.0中获得了NoSuchMethoodExcepton。

这是我的代码:

   @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   getMenuInflater().inflate(R.menu.actions , menu);
   }

   @Override
   public boolean onContextItemSelected(MenuItem item) {

   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

   switch(item.getItemId()){
       case R.id.single:
           showDatePickerDialog(temp1);

           break;
       case R.id.showtotal:
           Toast.makeText(this, "Delete : " , Toast.LENGTH_SHORT).show();
           break;


   }
   return true;
   }
   public void showDatePickerDialog(TextView t) {

   temp=t;
    DialogFragment newFragment = new DatePickerFragment(){
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            Calendar calendar= Calendar.getInstance();
            calendar.set(year, month, day);
            d1=new java.sql.Date(calendar.getTimeInMillis());
            temp.setText(""+d1);  
            Toast.makeText(GroupPage.this, "Date : "+temp1.getText() , Toast.LENGTH_SHORT).show();
            Intent i =new Intent(GroupPage.this,ShowPre.class);
            i.putExtra("gid", stid);
            i.putExtra("date", d1.toString());
            startActivity(i);

        }
     };
    newFragment.show(getSupportFragmentManager(), "datePicker");
   }
 } 

这是错误日志:

  05-09 12:45:29.617: E/AndroidRuntime(4338): FATAL EXCEPTION: main
  05-09 12:45:29.617: E/AndroidRuntime(4338): android.view.InflateException: Couldn't resolve menu item onClick handler showDatePickerDialog in class  com.example.attendence.GroupPage
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:202)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:402) 
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.MenuInflater.parseMenu(MenuInflater.java:173)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.MenuInflater.inflate(MenuInflater.java:95)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.example.attendence.GroupPage.onCreateContextMenu(GroupPage.java:117)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.View.createContextMenu(View.java:6194)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2162)
   05-09 12:45:29.617: E/AndroidRuntime(4338):  at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:570)
   05-09 12:45:29.617: E/AndroidRuntime(4338):  at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:570)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:570)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.View.showContextMenu(View.java:3587)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.app.Activity.openContextMenu(Activity.java:2732)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.example.attendence.GroupPage$3.onClick(GroupPage.java:103)
   05-09 12:45:29.617: E/AndroidRuntime(4338):  at android.view.View.performClick(View.java:3517)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.view.View$PerformClick.run(View.java:14155)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.os.Handler.handleCallback(Handler.java:605) 
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at android.os.Handler.dispatchMessage(Handler.java:92)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at android.os.Looper.loop(Looper.java:154)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at android.app.ActivityThread.main(ActivityThread.java:4624)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at java.lang.reflect.Method.invoke(Method.java:511)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at dalvik.system.NativeStart.main(Native Method)
 05-09 12:45:29.617: E/AndroidRuntime(4338): Caused by: java.lang.NoSuchMethodException: showDatePickerDialog [interface android.view.MenuItem]
  05-09 12:45:29.617: E/AndroidRuntime(4338):   at java.lang.Class.getConstructorOrMethod(Class.java:460)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    at java.lang.Class.getMethod(Class.java:915)
 05-09  12:45:29.617: E/AndroidRuntime(4338):   at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:200)
 05-09 12:45:29.617: E/AndroidRuntime(4338):    ... 25 more

1 个答案:

答案 0 :(得分:1)

您在xml布局中声明了onClick属性。

onClick="showDatePickerDialog"

onClick将View对象作为参数而不是TextView。所以你应该用这种方式改变方法签名

public void showDatePickerDialog(View t) 

或者如果您使用菜单

 public void showDatePickerDialog(MenuItem item)