我正在制作网格布局并希望将其放入警报对话框中,但是当我尝试使用 显示警报对话框show()
时,应用程序显示异常。
这是代码:
public class GridViewActivity extends Activity {
GridView gridView;
Context context;
static final String[] MOBILE_OS = new String[] { "timer", "about"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Toast.makeText(
// getApplicationContext(),
// ((TextView) v.findViewById(R.id.grid_item_label))
// .getText(), Toast.LENGTH_SHORT).show();
if(position == 0){
Toast.makeText(GridViewActivity.this, "first icon is pressed", Toast.LENGTH_SHORT).show();
}
else if(position == 1){
Toast.makeText(GridViewActivity.this, "second icon is pressed", Toast.LENGTH_SHORT).show();
}
}});
Dialog();
}
private void Dialog(){
AlertDialog alertDialog;
Context mContext = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main,null);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
这是一个例外:
07-21 14:59:51.832: E/AndroidRuntime(1683): FATAL EXCEPTION: main
07-21 14:59:51.832: E/AndroidRuntime(1683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.velosys.main/com.velosys.main.GridViewActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.os.Looper.loop(Looper.java:123)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-21 14:59:51.832: E/AndroidRuntime(1683): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 14:59:51.832: E/AndroidRuntime(1683): at java.lang.reflect.Method.invoke(Method.java:507)
07-21 14:59:51.832: E/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-21 14:59:51.832: E/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-21 14:59:51.832: E/AndroidRuntime(1683): at dalvik.system.NativeStart.main(Native Method)
07-21 14:59:51.832: E/AndroidRuntime(1683): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.view.ViewRoot.setView(ViewRoot.java:531)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.Dialog.show(Dialog.java:241)
07-21 14:59:51.832: E/AndroidRuntime(1683): at com.velosys.main.GridViewActivity.Dialog(GridViewActivity.java:58)
07-21 14:59:51.832: E/AndroidRuntime(1683): at com.velosys.main.GridViewActivity.onCreate(GridViewActivity.java:48)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-21 14:59:51.832: E/AndroidRuntime(1683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-21 14:59:51.832: E/AndroidRuntime(1683): ... 11 more
我怎么解决这个问题?