0

enter code here我正在尝试在我的 Android 应用程序中放置一个进度对话框,但我遇到了一些麻烦。我是 Android 新手,所以我正在关注本教程: http: //p-xr.com/android-tutorial-how-to-make-a-progress-dialog/

当我在手机上测试我的应用程序时,它所做的只是强制关闭。有人可以帮忙吗?这是我的 .Java 活动:

public class XXXXXActivity extends Activity {
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView myWebView=(WebView)findViewById(R.id.webview);
    myWebView.addJavascriptInterface(new JavaScriptInterface(this),"Android");
    WebSettings websettings =
            myWebView.getSettings();
    websettings.setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient());
    myWebView.loadUrl("http://XXXXX.com");
}

private void runDialog(final int seconds)
{
    progressDialog = ProgressDialog.show(this, "Please wait...", "Loading...");
    new Thread(new Runnable(){
        public void run(){
           try {
                        Thread.sleep(seconds * 1000);
                progressDialog.dismiss();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
public class JavaScriptInterface {
    Context mContext;
    /** Instantiate the interface and set the context*/
    JavaScriptInterface(Context c) {
        mContext = c;
    }
    /** Show a toast from the web page*/
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        view.loadUrl(url); 
        if
        (Uri.parse(url).getHost().equals("XXXXX.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent=new
                Intent (Intent.ACTION_VIEW,Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

日志猫:

[2012-04-13 19:44:55 - XXXXX] ------------------------------
[2012-04-13 19:44:55 - XXXXX] Android Launch!
[2012-04-13 19:44:55 - XXXXX] adb is running normally.
[2012-04-13 19:44:55 - XXXXX] No Launcher activity found!
[2012-04-13 19:44:55 - XXXXX] The launch will only sync the application package   on the device!
[2012-04-13 19:44:55 - XXXXX] Performing sync
[2012-04-13 19:44:55 - XXXXX] Automatic Target Mode: Preferred AVD 'MY-PHONE' is not available. Launching new emulator.
[2012-04-13 19:44:55 - XXXXX] Launching a new emulator with Virtual Device 'MY-PHONE'
[2012-04-13 19:45:08 - Emulator] emulator: WARNING: Unable to create sensors port: Unknown error
[2012-04-13 19:45:08 - XXXXX] New emulator found: emulator-5554
[2012-04-13 19:45:08 - XXXXX] Waiting for HOME ('android.process.acore') to be launched...
[2012-04-13 19:46:05 - XXXXX] emulator-5554 disconnected! Cancelling 'sync'!
4

1 回答 1

0

我看到一个问题:您在非 gui 线程中关闭对话框:

new Thread(new Runnable(){
        public void run(){
           try {
                        Thread.sleep(seconds * 1000);
                progressDialog.dismiss();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

这段代码很奇怪,我认为你应该重写逻辑。

于 2012-04-13T18:34:34.993 回答