在dialog.show()中崩溃

时间:2011-04-28 16:15:10

标签: android crash camera android-camera

我正在使用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]  

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

2 个答案:

答案 0 :(得分:10)

如果您在活动中(例如MyActivity),则可以创建对话框:

Dialog dialog = new Dialog(this);

或者如果你在Activity的内部类中:

Dialog dialog = new Dialog(MyActivity.this);

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

答案 1 :(得分:-1)

如果您在dialog.show();上获得例外 试试context.dialog.show();

如果出现持续现象,请检查您的背景信息。