0

我正在尝试观看带有两个选项的 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();

         } 
});
}

怎么了?

谢谢。

4

2 回答 2

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();

     } 
于 2013-08-15T20:50:50.783 回答
1

更改此行

AlertDialog.Builder(getApplicationContext());

AlertDialog.Builder(Insertarlugar.this);

ADialog是在 上创建的,Activity因此您想使用它Context来创建它,而不是应用程序的Context. UI据我了解,对于大多数对象都是如此Context。有时这可能适用于某些对象,例如 aToast但是,在我看来,最好尝试使用正确的Context,这里是Activity's。

并查看有关何时使用 which 的答案Context

于 2013-08-15T20:50:05.957 回答