Android 更改时来自progressBar的WebView NullPointerException
尽管它在我的设备和其他设备上运行良好,但在发布时,我似乎得到了大量用户的NullPointerException,我用一个try-catch来阻止崩溃,但我仍然收到大量报告Android 更改时来自progressBar的WebView NullPointerException,android,webview,crash,nullpointerexception,progressdialog,Android,Webview,Crash,Nullpointerexception,Progressdialog,尽管它在我的设备和其他设备上运行良好,但在发布时,我似乎得到了大量用户的NullPointerException,我用一个try-catch来阻止崩溃,但我仍然收到大量报告 0java.lang.NullPointerException 1at broakenmedia.wiredmagazine.MainActivity$loadWebsite$1.onProgressChanged(MainActivity.java:331) 2at android.webkit.CallbackProxy
0java.lang.NullPointerException
1at broakenmedia.wiredmagazine.MainActivity$loadWebsite$1.onProgressChanged(MainActivity.java:331)
2at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:359)
3at android.os.Handler.dispatchMessage(Handler.java:99)
4at android.os.Looper.loop(Looper.java:132)
5at android.app.ActivityThread.main(ActivityThread.java:4126)
6at java.lang.reflect.Method.invokeNative(Native Method)
7at java.lang.reflect.Method.invoke(Method.java:491)
8at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
9at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
10at dalvik.system.NativeStart.main(Native Method)
代码:
页面加载看起来是空的-如何声明UI元素?ProgressBar页面加载;就在onCreate之前,是否将其设置为静态?但是为什么它可以在某些设备上工作,而不能在其他设备上工作呢?是的,但是xml?您是否在充气时以编程方式将其添加到视图中?哦,对不起,不是,它是在XML中声明的,这可能与它是ASyncTask的一部分有关吗?可能-您可以尝试使用构造函数将WebView和ProgressBar传递到LoadWebsite类中,而不是在内部类中调用findViewById
@SuppressWarnings("rawtypes")
private class loadWebsite extends AsyncTask{
private WebView webview;
@Override
protected Object doInBackground(Object... arg0) {
webview = (WebView)findViewById(R.id.webView);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(final WebView view, final int progress) {
if(progress==100){
try {
pageLoading = (ProgressBar) findViewById(R.id.pageLoading);
pageLoading.setVisibility(View.GONE);
} catch (Exception e) {
// TODO Auto-generated catch block
BugSenseHandler.log("ProgressBarAt100", e);
}
}
else
{
try {
pageLoading = (ProgressBar) findViewById(R.id.pageLoading);
pageLoading.setVisibility(View.VISIBLE);
} catch (Exception e) {
// TODO Auto-generated catch block
BugSenseHandler.log("ProgressBarAt0", e);
}
}
//progressDialog.incrementProgressBy(progress);
//if(progress == 100 && progressDialog.isShowing())
// progressDialog.dismiss();
}
});
webview = (WebView)findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient(){
@Override
public void onLoadResource(WebView view, String url){
}
public void onReceivedError(final WebView view, final int errorCode,
final String description, final String failingUrl) {
if (errorCode == ERROR_TIMEOUT) {
view.stopLoading(); // may not be needed
Toast.makeText(MainActivity.this, getString(R.string.an_error_occured_couldnt_load_page),
Toast.LENGTH_SHORT).show();
//view.loadData(timeoutMessageHtml, "text/html", "utf-8");
}
}
@Override
public void onPageFinished(WebView view, String url) {
try {
pageLoading = (ProgressBar) findViewById(R.id.pageLoading);
pageLoading.setVisibility(View.GONE);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
webview.getSettings().setSupportZoom(true);
//webview.getSettings().setBuiltInZoomControls(true);
webview.loadUrl("http://m.wired.com");
return webview;
}
}