2013-04-04 53 views
1

我有那個顯示兩次的AlertDialog,我不知道爲什麼!該問題不適用於AlertDialogs;但是,它也適用於"Activities"Activity和AlertDialog正在啓動兩次

請注意,我面臨Android 4.0.3的問題。但是,當我在Android 2.3.6上運行應用程序時,一切正常。

爲了解決我的問題Activitie s,我已經在清單文件中設置: android:launchMode="singleInstance"它工作。

但是,這不能爲AlertDialog s完成,因爲它們在清單文件中沒有任何引用來將它設置爲singleInstance或類似的東西。

有人告訴我把BreakPoint爲了檢查我的代碼後show()。但我不知道如何把BreakPoint以及如何檢查。

編輯:

我使用HoloEverywhere和SherlockActionBar。我不知道他們有多少效果。

@Override 
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { 
    switch(item.getItemId()) 
    { 
    case R.id.action_one: 
     alertDialog(); 
     break; 
    case R.id.action_two: 
     Intent i = new Intent(this,Info.class); 
     startActivity(i); 
     overridePendingTransition(0, 0);  
     break; 

    } 
    return super.onOptionsItemSelected(item); 
} 

private void alertDialog(){ 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("My title"); 
     AlertDialog alert= builder.create(); 
     alert.show(); 

} 

case R.id.action_two它在清單文件中設置launchMode="singleInstance"後工作正常。然而,在case R.id.action_one啓動AlertDialog它仍然打開兩次。

@Override 
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
     // TODO Auto-generated method stub 
     com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.action_options, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
+0

發佈您的代碼。 – Tushar 2013-04-04 00:33:31

+0

@Tushar發佈:) – Alex 2013-04-04 00:46:15

+0

你也可以發佈你的'onCreateOptionsMenu'和'onPrepareOptionsMenu'嗎? – Tushar 2013-04-04 00:48:24

回答

1

改變這一行:

return super.onCreateOptionsMenu(menu); 

到:

return true; 

當你調用super.onCreateOptionsMenu,它增加附加onMenuItemClickListener聽衆菜單中的每個項目,這樣就會造成2次點擊被記錄。 (PS:我其實已經想通了,打算髮布它,但不得不接電話。沒有笑話)

+0

謝謝您的解釋Tushar! :) – Alex 2013-04-04 01:51:23

+0

@JonathanHugh對不起,以前不能給你答案,但我認爲你最好獨立發現它。祝你的程序好運! – Tushar 2013-04-04 02:00:34