5

我正在使用 getApplicationContext() 函数创建一个对话框,当我调用 dialog.show() 时,这会导致程序崩溃。我正在使用 getApplicationContext() 因为我试图在 Camera.PictureCallback() 中打开对话框,如下所示:

Camera.PictureCallback pictureCallbackJpeg = new Camera.PictureCallback()
{
    public void onPictureTaken(byte[] data, Camera c)
    {
        Context context = getApplicationContext();
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.send_dialog);
        dialog.setTitle("Send image?");
        dialog.show();

        camera.startPreview();
    }
};

这是崩溃日志:

Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))   
Dialog.show() line: 245 
myApp$1.onPictureTaken(byte[], Camera) line: 31 
Camera$EventHandler.handleMessage(Message) line: 328    
Camera$EventHandler(Handler).dispatchMessage(Message) line: 99  
Looper.loop() line: 143 
ActivityThread.main(String[]) line: 4914    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  

任何想法如何解决这一问题?

4

2 回答 2

10

如果你在一个活动中(比如MyActivity),你可以创建对话框:

Dialog dialog = new Dialog(this);

或者,如果您在 an 的内部类中Activity

Dialog dialog = new Dialog(MyActivity.this);

否则你可以试试getBaseContext().
您只需确保您在 UI 线程中工作。

于 2011-04-28T16:20:22.327 回答
-1

dialog.show(); 如果您在尝试时遇到异常context.dialog.show();

如果持续发生,请检查您的上下文。

于 2011-04-28T16:22:26.120 回答