標籤:android style blog http io os ar 使用 java
原部落格地址:http://aijiawang-126-com.javaeye.com/blog/662336
在Activity中newSpinner是我把mContext傳入,但是出了 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application這個錯誤,參考了達人的文章終於搞定。
[java] view plaincopy
- private Context mcontext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
- System.out.println("mcontext=" + mcontext);
- }
[java] view plaincopy
- new AlertDialog.Builder(mcontext)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Warnning")
- .setMessage(
- "You forget to write the message. Do you want to fill out it ??")
- .setPositiveButton("Yes", positiveListener).setNegativeButton(
- "No", negativeListener).create().show();
導致報這個錯是在於new AlertDialog.Builder(mcontext),雖然這裡的參數是AlertDialog.Builder(Context context)但我們不能使用getApplicationContext()獲得的Context,而必須使用Activity,因為只有一個 Activity才能添加一個表單。
解決方案:將new AlertDialog.Builder(Context context)中的參數用Activity.this(Activity是你的Activity的名稱)來填充就可以正確的建立一個Dialog了。
[java] view plaincopy
- new AlertDialog.Builder(MyActivity.this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Warnning")
- .setMessage(
- "You forget to write the message. Do you want to fill out it ??")
- .setPositiveButton("Yes", positiveListener).setNegativeButton(
- "No", negativeListener).create().show();
- 上一篇android自訂標題列progressBar
- 下一篇android橫豎屏切換的一點感想
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application