我有一个带有WeView的Activity,它加载了一个url,我希望在加载网站时显示一些等待对话框,所以我试过这个:
private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.web_view_activity);
WebView wv;
wv = (WebView) findViewById(R.id.areaWebSolver);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
});
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
wv.loadUrl(url);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
}
不幸的是不起作用,应用程序崩溃了source not found
...如果我尝试删除进度对话框代码,则活动有效。怎么了?我怎么能解决这个问题?
答案 0 :(得分:0)
您可以在ThreadPolicy
方法之后出于任何原因设置setContentView()
,并且您的进度对话框永远不会获得Context
,而您下面的语句将根本不会加载活动上下文。< / p>
private ProgressDialog dialog = new ProgressDialog(this);
找到适合我的伪代码。
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view_activity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
dialog = new ProgressDialog(this);
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
WebView wv = (WebView) findViewById(R.id.areaWebSolver);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
...
}
未找到的源可能是由于您链接的其他一些外部库,而且它们在IDE的类路径中不可用。 READ HERE了解更多信息。
答案 1 :(得分:0)
此
private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);
//This will give you NullPointerException.
// You can use the activity context after you set the content to the activity.
应该是
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.web_view_activity);
dialog = new ProgressDialog(MyNameActivity.this);
....
}
答案 2 :(得分:0)
您应该在网络视图活动中使用进度条
// Just find
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//in onCreate Method of activity and than after
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
//Progress bar gone in onFinished method of webview.
<强> XML:强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webview01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
答案 3 :(得分:0)
我认为这会对你有帮助。
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view_activity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
WebView wv = (WebView) findViewById(R.id.areaWebSolver);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
dialog = new ProgressDialog(this);
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
if(!dialog.showing()){
view.loadUrl(url);
dialog.show();
return true;
}
}
@Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
...
}