我正在制作网格布局,并希望将其置于警告对话框中,但当我尝试使用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
我该如何解决这个问题?
答案 0 :(得分:7)
使用当前活动上下文而不是应用程序上下文
Context mContext = GridViewActivity.this;
而不是
Context mContext = getApplicationContext();
答案 1 :(得分:3)
更改此行
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
到
AlertDialog.Builder builder = new AlertDialog.Builder(GridViewActivity.this);
答案 2 :(得分:2)
使用活动的context
而不是使用应用程序的上下文作为getApplicationContext()
,因此请使用Activity_Name.this
。