本文介紹了在非活動類中顯示進度對話框的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試在非 Activity 類中顯示對話框.基本上,我在我的應(yīng)用程序中檢測到一個對象,我想顯示一個對話框然后切換活動.我在我的 logcat 中收到j(luò)ava.lang.RuntimeException:無法在未調(diào)用 Looper.prepare() 的線程內(nèi)創(chuàng)建處理程序".
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.
這是我的一些代碼:
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++;
}
推薦答案
你的 Dialog 應(yīng)該從 UIthread 調(diào)用,所以嘗試使用它,
Your Dialog should be called from the UIthread so try to use this,
context.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mDialog.show();
}
});
希望這行得通.
這篇關(guān)于在非活動類中顯示進度對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!