Android 在dialog.show() 中崩溃

发布于 2024-11-03 18:14:39 字数 1287 浏览 2 评论 0原文

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

有什么想法如何解决这个问题吗?

I am creating a dialog using the getApplicationContext() function and this causes the program to crash when I call dialog.show(). I am using getApplicationContext() because I am trying to make the dialog open within a Camera.PictureCallback() like this:

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

Here is the crash log:

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]  

Any ideas how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-11-10 18:14:40

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

如果仍然存在,请检查您的上下文。

If your getting exception on dialog.show();
try context.dialog.show();

If persists occurs check your context also.

海风掠过北极光 2024-11-10 18:14:39

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

Dialog dialog = new Dialog(this);

或者如果您位于 Activity 的内部类中:

Dialog dialog = new Dialog(MyActivity.this);

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

If you are inside an activity (say MyActivity), you can create the Dialog:

Dialog dialog = new Dialog(this);

or if you are inside an inner class of an Activity:

Dialog dialog = new Dialog(MyActivity.this);

Otherwise you could try the getBaseContext().
You just make sure, that you're working in the UI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文