0

当我尝试在手机上运行该程序时,它一直在崩溃。无法弄清楚为什么它会崩溃

主java

public class ProgressBar extends Activity {

WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main );
 final Activity MyActivity = this;

// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress)   
    {
     //Make the bar disappear after URL is loaded, and changes string to Loading...
    MyActivity.setTitle("Loading...");
     MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded

     // Return the app name after finish loading
        if(progress == 100)
           MyActivity.setTitle(R.string.app_name);
      }
    });

webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com/");

}
private class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.loadUrl(url);
    return true;
    }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
    webview.goBack();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
4

1 回答 1

2

您将希望从崩溃中找到堆栈跟踪,您可以使用 logcat 获取它,或者在 eclipse 中打开 logcat 视图(或打开 DDMS 透视图),或者adb logcat从 shell 运行以查看日志。

不过,在这种情况下,我很确定您会看到任何错误,说明您需要在创建任何内容之前设置窗口功能,将您的setContentView呼叫移至呼叫之后getWindow().requestFeature

于 2010-12-02T06:41:45.500 回答