2012-04-13 66 views
0

enter code here我正在嘗試在我的Android應用程序中放置一個進度對話框,並且我遇到了一些問題。我是Android新手,因此我正在學習以下教程: http://p-xr.com/android-tutorial-how-to-make-a-progress-dialog/Android Activity Force Close Issue

當我在手機上測試我的應用程序時,它所做的只是強制關閉。 任何人都可以幫忙嗎? 繼承人我的.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; 
    } 
} 

的logcat:

[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'! 
+0

請發佈您的logcat輸出。 – 2012-04-13 18:32:05

回答

0

我看到一個問題:你在非GUI線程解僱對話框:

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

這段代碼很奇怪所有,我認爲你應該重寫邏輯。

+0

這不應該是問題,因爲他沒有在代碼中的任何地方調用runDialog函數。 – San 2012-04-13 18:56:24

+0

@AlexKlimashevsky有沒有可能找到一個鏈接或什麼重寫代碼?正如我在我的問題中所說的,我是Android新手。 – SMOKE 2012-04-13 19:38:41

+0

@SaneeshCS我是否需要在代碼中調用runDialog函數?正如我在我的問題中所說的,我是Android新手。這也是所有的教程告訴我這樣做,我想知道如果那傢伙遺漏了什麼? – SMOKE 2012-04-13 19:43:08