本文介绍了非活性类显示progressdialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示在非Activity类的对话框。基本上,我发现一个物体在我的应用程序,我想显示一个对话框,然后切换活动。我得到一个了java.lang.RuntimeException:内螺纹已经不叫尺蠖prepare()不能创建处理程序。在我的logcat
下面是我的一些code:
公共ImageTargetsRenderer(上下文的背景下){ this.context =背景; mDialog =新ProgressDialog(上下文); } 公共无效onDrawFrame(GL10 GL){ testFlag = 0; //不会使,如果没有可跟踪 如果(!mIsActive) 返回; //调用我们的本地函数来呈现内容 //渲染,如果有可跟踪 testFlag = renderFrame(); 通信System.err.println(ImageTargetsRenderer报道:+ testFlag); 如果(testFlag大于0&安培;&安培; frameCount→5) { frameCount = 0; 通信System.err.println(启动切换的活动。); mDialog.setTitle(请稍候); mDialog.setMessage(请稍候); mDialog.show(); 新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(5000); }赶上(例外五){} //关闭该对话框 mDialog.dismiss(); } }。开始(); 意图myIntent =新的意图(背景下,FlashActivity.class); myIntent.putExtra(com.qualcomm.QCARSamples.ImageTargets.flagTest,testFlag); myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); testFlag = 0; 返回; } frameCount ++; }解决方案
您对话应该从UIthread调用,所以尝试使用这个,
context.this.runOnUiThread(新的Runnable(){ @覆盖 公共无效的run(){ mDialog.show(); } });
希望这个作品。
I am trying to display a dialog in a non-Activity class. Basically, I detect an object in my app, I would like to display a dialog and then switch activities. I'm getting a "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()" in my logcat.
Here is some of my code:
public ImageTargetsRenderer(Context context) { this.context = context; mDialog = new ProgressDialog(context); } public void onDrawFrame(GL10 gl) { testFlag = 0; // DO NOT RENDER IF THERE IS NO TRACKABLE if (!mIsActive) return; // Call our native function to render content // RENDER IF THERE IS A TRACKABLE testFlag = renderFrame(); System.err.println("ImageTargetsRenderer reports: " + testFlag); if(testFlag > 0 && frameCount > 5) { frameCount = 0; System.err.println("Starting to switch activities."); mDialog.setTitle("Please wait"); mDialog.setMessage("Please wait"); mDialog.show(); new Thread() { public void run() { try{ sleep(5000); } catch (Exception e) { } // Dismiss the Dialog mDialog.dismiss(); } }.start(); Intent myIntent = new Intent(context, FlashActivity.class); myIntent.putExtra("com.qualcomm.QCARSamples.ImageTargets.flagTest", testFlag); myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); context.startActivity(myIntent); testFlag = 0; return; } frameCount++; }解决方案
Your Dialog should be called from the UIthread so try to use this,
context.this.runOnUiThread(new Runnable() { @Override public void run() { mDialog.show(); } });Hope this works.
更多推荐
非活性类显示progressdialog
发布评论