2012-04-12 45 views
0

請看看下面的代碼... 我正在創建一個上下文菜單並實現方法onContextItemSelected但問題是,當我按下回復項...刪除對話框情況也出現和活動也開始兩次......意味着所有的情況下,執行...在刪除回覆和轉發的情況下...會發生什麼是問題,請幫助上下文菜單不能正常工作

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     // TODO Auto-generated method stub 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     switch(item.getItemId()) 
     { 
    case R.id.reply: 
    { 
      Intent i = new Intent(); 
      String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
        .getString(2); 
      i.putExtra("number", s2); 
     // Log.v("number", s2); 
      i.setClass(this, CreateMessage.class); 
      // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(i); 

    } 
     case R.id.delete: 
     { 

     final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
         .getString(1); 

       dba = new DBAdapter(this); 

       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) { 
          // Log.v("", "You Clicked " + s);    


          dba.delete("messages", "id=?", new String[] { s }); 
          populate(); 
          dba.close(); 
          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 
     } 

     case R.id.forward: 
     { 
      Intent i = new Intent(); 
      String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
        .getString(4); 
     // Log.v("message", s3); 
      i.putExtra("message", s3); 
      i.setClass(this, CreateMessage.class); 
      startActivity(i); 
     } 
default: 

     return super.onContextItemSelected(item); 
     } 
    } 

這裏是在logcat中顯示的logcat錯誤...

03-30 09:13:28.439: E/WindowManager(2273): Activity sms.app.Displayer has leaked window [email protected] that was originally added here 

sms.app.Displayer是在我在執行上下文菜單中的類..

,這裏是上下文菜單文件的代碼..

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 


    <item android:id="@+id/reply" android:title="Reply"></item><item 
     android:id="@+id/forward" 
     android:title="Forward"> 
    </item> 
    <item android:id="@+id/delete" android:title="Delete Message"> 
    </item> 

</menu> 

回答

0

一旦檢查這個代碼的微小變化在你onContextItemSelected方法::

@Override 
    public boolean onContextItemSelected(MenuItem item) { 
     // TODO Auto-generated method stub 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     switch(item.getItemId()){ 
     case R.id.reply: 
      Intent i = new Intent(); 
      String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
        .getString(2); 
      i.putExtra("number", s2); 
      // Log.v("number", s2); 
      i.setClass(this, CreateMessage.class); 
      // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(i); 
      break; 
     case R.id.delete: 

      final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
        .getString(1); 

      dba = new DBAdapter(this); 

      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) { 
        // Log.v("", "You Clicked " + s);    


        dba.delete("messages", "id=?", new String[] { s }); 
        populate(); 
        dba.close(); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     break; 

     case R.id.forward: 
      Intent i = new Intent(); 
      String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position)) 
        .getString(4); 
      // Log.v("message", s3); 
      i.putExtra("message", s3); 
      i.setClass(this, CreateMessage.class); 
      startActivity(i); 
     break; 
     } 
     return true; 
    } 
+0

是這是正確的,但返回真正的聲明應該在外部範圍....不是在break語句後 – kashifmehmood 2012-04-12 09:42:29

+0

編輯我的答案根據需要/建議 – 2012-04-12 09:52:42

1

你的case語句

後加上休息

編輯: 由於缺少中斷,因此您可能會遇到以下情況。或者給你的case語句添加中斷(以後再處理返回值)或者直接添加返回true而不是中斷。

+0

休息時間給出了一個錯誤,該方法必須返回一個boolean – kashifmehmood 2012-04-12 09:22:21

+0

再加入真正的回報,而不是斷裂 – 2012-04-12 09:23:15

+0

同樣的錯誤,如果我添加返回true to all cases – kashifmehmood 2012-04-12 09:25:59