2013-11-22 65 views
1

我的應用程序,完美的工作沒有這部分代碼:Android的 - 動作條選項卡+對話框

if(tab.getPosition() == 4) 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
     builder.setTitle("Alert Dialog"); 
     builder.setMessage("This is Example of Alert Dialog with three Buttons"); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "Ok is clicked", Toast.LENGTH_LONG).show(); 

      } 
     }); 
     builder.show(); 
    } 

就是裏面的

public void onTabSelected(Tab tab, FragmentTransaction ft) { 

我有5個標籤+ 1應該只顯示對話框...當我點擊這個標籤時,我的應用會崩潰,並顯示錯誤:

11-22 14:32:00.818: E/AndroidRuntime(17853): FATAL EXCEPTION: main 
11-22 14:32:00.818: E/AndroidRuntime(17853): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.view.ViewRootImpl.setView(ViewRootImpl.java:797) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.app.Dialog.show(Dialog.java:287) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.app.AlertDialog$Builder.show(AlertDialog.java:951) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.iecom.eresitev.WeekActivity.onTabSelected(WeekActivity.java:411) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:603) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1145) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:658) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.view.View.performClick(View.java:4475) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.view.View$PerformClick.run(View.java:18786) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.os.Handler.handleCallback(Handler.java:730) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.os.Handler.dispatchMessage(Handler.java:92) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.os.Looper.loop(Looper.java:137) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at android.app.ActivityThread.main(ActivityThread.java:5419) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at java.lang.reflect.Method.invokeNative(Native Method) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at java.lang.reflect.Method.invoke(Method.java:525) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
11-22 14:32:00.818: E/AndroidRuntime(17853): at dalvik.system.NativeStart.main(Native Method) 
+0

而不是使用'getApplicationContext'好心嘗試使用'this'或'WeekActivity.this'從那裏你打這個 –

+0

?如果你正在使用一個片段,確保你在onCreateView之後調用它,一個稱爲它的好地方是onActivityCreated,你也可以用android.R.string.ok代替「Ok」。 –

+0

@MehulJoisar哇..謝謝你。 ..應該getApplicationContext或getBaseContext的工作?如果您如此善良,並且在答案中對所有3種方法進行了一點說明,我一定會接受並回答您+1!謝謝! –

回答

相關問題