2013-05-09 125 views
0

我的應用程序有一個按鈕,單擊時顯示上下文菜單。點擊第一個itema時,會打開一個日期選擇器對話框片段。Android上的NoSuchMethodException上下文菜單

它在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 
+0

什麼是你的菜單文件一樣改變方法的簽名?你可能會在那裏使用一些被棄用的屬性。 – 2013-05-09 07:27:05

回答

1

您聲明一個xml佈局內的onClick屬性。

onClick="showDatePickerDialog" 

onClick將參數視爲一個View對象而不是TextView。所以,你應該用這種方式

public void showDatePickerDialog(View t) 

,或者如果您使用的是菜單

public void showDatePickerDialog(MenuItem item) 
+0

我用MenuItem項作爲參數,它的工作。 thnx很多:)你可以告訴我爲什麼視圖v不能在android 4.0中工作? – 2013-05-09 07:42:53