运行时进度条错误

发布于 2024-10-05 18:19:52 字数 2317 浏览 0 评论 0原文

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

Main 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>

The Program keeps crashing when i try to run it on my handset. cant figure out why it is crashing

Main 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-10-12 18:19:52

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

但在这种情况下,我很确定您会看到任何错误,指出您需要在创建任何内容之前设置窗口功能,请将 setContentView 调用移至 getWindow() 之后。 requestFeature 调用。

You'll want to find the stacktrace from the crash, you can get this with logcat, either open the logcat view in eclipse (or open the DDMS perspective), or run adb logcat from the shell to see the log.

In this case though, i'm pretty sure you'll see any error saying you need to set the window feature before creating any content, move your setContentView call to after the getWindow().requestFeature call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文