2011-03-03 9 views
9

मैं संदर्भ मेनू के लिए निम्न कोड का उपयोग कर रहा हूं और फिर यदि उपयोगकर्ता हटा दिया गया है, तो एक संवाद मालिश दिखाई देगी।

infos.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){ 
      //@Override 
      public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
       menu.setHeaderTitle("Context Menu"); 
       menu.add(0, CONTEXT_EDIT, 0, "Edit Item"); 
       menu.add(0, CONTEXT_DELETE, 1, "Delete Item"); 
      } 
}); 

public boolean onContextItemSelected(MenuItem item) { 

     AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
     final Long _id = menuInfo.id; 
     //selected_row = menuInfo.position; 

     // To get the id of the clicked item in the list use menuInfo.id 
     switch (item.getItemId()) { 
      case CONTEXT_EDIT: 
       addEditRes(_id); 
       break; 
      case CONTEXT_DELETE: 
       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setMessage("Are you sure you want to delete?") 
         .setCancelable(false) 
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           infoDataHelper.deleteRes(_id); 
           model = infoDataHelper.getCursor(addType); 
           adapter.changeCursor(model); 
          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
       break; 
      default: 
       return super.onContextItemSelected(item); 

     } 
     adapter.notifyDataSetChanged(); 
     return true; 
} 

लेकिन जैसे ही मेरे द्वारा हटाए जाने को चुनने कर रहा हूँ, यह निम्न त्रुटि दे रहा है।

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

मेरे कोड में समस्या क्या है?

उत्तर

6

यह अलर्टडिअलॉग.बिल्डर बिल्डर = नया अलर्टडिअलॉग.बिल्डर (यह .getParent()) होना चाहिए;

क्योंकि गतिविधि किसी अन्य टैबैक्टिविटी के भीतर एक सारणीयता में है।

28

मेरा मानना ​​है कि समस्या इस लाइन पर हो सकता है:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

करने के लिए इसे संशोधित करने का प्रयास करें:

AlertDialog.Builder builder = new AlertDialog.Builder(MyActivityName.this); 

अपनी गतिविधि के नाम के साथ MyActivityName की जगह।

क्या इससे त्रुटि ठीक हुई?

+0

मेरे लिए इसे ठीक करें! धन्यवाद :) –

6

मुझे एक ही त्रुटि मिल रही थी। मैं

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 

अब ठीक काम कर रहा इसके लिए

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

बदल दिया है। धन्यवाद।

संबंधित मुद्दे