我正在尝试使用两种选择来观看AlertDialog:Cámara和Galería。但是当我运行应用程序时,它会关闭并显示消息“不幸的是,MapaLugares已关闭”。
logcat消息是:
08-15 20:37:02.960: E/AndroidRuntime(5377): FATAL EXCEPTION: main
08-15 20:37:02.960: E/AndroidRuntime(5377):
android.view.WindowManager$BadTokenException: Unable to add window -- token null is
not for an application
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.ViewRootImpl.setView(ViewRootImpl.java:521)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.app.Dialog.show(Dialog.java:289)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
com.app.mapa.Insertarlugar$1.onClick(Insertarlugar.java:62)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.View.performClick(View.java:3521)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.view.View$PerformClick.run(View.java:14185)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.os.Handler.handleCallback(Handler.java:605)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.os.Handler.dispatchMessage(Handler.java:92)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.os.Looper.loop(Looper.java:137)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
android.app.ActivityThread.main(ActivityThread.java:4464)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
java.lang.reflect.Method.invokeNative(Native Method)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
java.lang.reflect.Method.invoke(Method.java:511)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
08-15 20:37:02.960: E/AndroidRuntime(5377): at
dalvik.system.NativeStart.main(Native Method)
08-15 20:37:05.210: I/ActivityThread(5573): Pub com.app.mapa.MCProvider:
com.app.mapa.MCProvider
08-15 20:37:05.570: D/memalloc(5573): /dev/pmem: Mapped buffer base:0x5127b000
size:2826240 offset:1290240 fd:51
08-15 20:37:05.620: E/Adreno200-ES20(5573): <qgl2DrvAPI_glUseProgram:1318>: **** 5573:
glUseProgram(3)
08-15 20:37:05.650: E/Adreno200-ES20(5573): <qgl2DrvAPI_glUseProgram:1318>: **** 5573:
glUseProgram(6)
08-15 20:37:05.670: D/memalloc(5573): /dev/pmem: Mapped buffer base:0x516f7000
size:4362240 offset:2826240 fd:58
代码:
public class Insertarlugar extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insertarlugar);
ImageButton imgbutton;
imgbutton = (ImageButton) findViewById(R.id.imgButton);
EditText titlugar;
EditText desclugar;
titlugar = (EditText) findViewById(R.id.edit_titulo_lugar);
String tlugar = titlugar.getText().toString();
desclugar = (EditText) findViewById(R.id.edit_descripcion_lugar);
String dlugar = desclugar.getText().toString();
imgbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String[] items = {"Cámara", "Galería"};
AlertDialog.Builder builder = new
AlertDialog.Builder(getApplicationContext());
builder.setTitle("Foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(Insertarlugar.this, "Click\n" + item,
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
怎么了?
感谢。
答案 0 :(得分:1)
更改此行
AlertDialog.Builder(getApplicationContext());
到
AlertDialog.Builder(Insertarlugar.this);
在Dialog
上创建了Activity
,因此您希望使用其Context
创建它,而不是应用程序的Context
。大多数UI
个对象都是如此,据我所知Context
。有时这可能适用于某些对象,例如Toast
,但在我看来,最好尝试使用正确的Context
,这里是Activity
。
请参阅this answer了解何时使用Context
答案 1 :(得分:1)
正如其他人在评论中提到的,你的背景是错误的。值得庆幸的是,使用View.getContext()方法很容易解决这个问题。
public void onClick(View arg0) {
final String[] items = {"Cámara", "Galería"};
AlertDialog.Builder builder = new
AlertDialog.Builder(arg0.getContext());
builder.setTitle("Foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(Insertarlugar.this, "Click\n" + item,
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}