0

My app has a button which when clicked, shows a context menu. On clicking the first itema a datepicker dialog fragment opens.

It's working in Android 2.3 but i am getting NoSuchMethoodExcepton in Android 4.0.

Here is my code:

   @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");
   }
 } 

and here is the error log:

  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
4

1 回答 1

1

you are declaring the onClick property inside an xml layout.

onClick="showDatePickerDialog"

the onClick takes as parameter a View object not a TextView. So you should change the method signature in this way

public void showDatePickerDialog(View t) 

or if you are using a menu

 public void showDatePickerDialog(MenuItem item) 
于 2013-05-09T07:25:45.667 回答