您好我有这个问题,我根本不明白我有一个标签式活动,每个片段都有一个webview,webviews有一个进度条,显示它们何时加载但是当我设置应用程序崩溃的进度条可见性AVD日志显示没有例外但是当我评论时
progressBar.setVisibility(View.GONE);
它根本不会崩溃,但是如果有人能告诉我问题在哪里我会非常感激,那么进度条总是在那里,谢谢你这是我的代码
package org.lsvbdarija.app;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/**
* Created by Anas on 20/06/2016.
*/
public class SocialFragment extends Fragment {
public WebView mWebView;
ProgressBar progressBar;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case 1: {
webViewGoBack();
}
break;
}
}
};
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
if (!haveNetworkConnection()) {
getActivity().setContentView(R.layout.activity_splashscreen);
}
View v = inflater.inflate(R.layout.social_layout, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
mWebView.loadUrl("http://www.lsvbdarija.com/search/label/%D8%B9%D9%84%D9%88%D9%85");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient(){
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String url2="http://www.lsvbdarija.com/";
// all links with in ur site will be open inside the webview
//links that start ur domain example(http://www.example.com/)
if (url != null && url.startsWith(url2)){
return false;
}
// all links that points outside the site will be open in a normal android browser
else {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
// progressBar.setVisibility(View.GONE);
}
});
mWebView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
handler.sendEmptyMessage(1);
return true;
}
return false;
}
});
return v;
}
private void webViewGoBack() {
mWebView.goBack();
}
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
}
答案 0 :(得分:1)
是否在片段布局或活动布局中定义了进度条?如果它在片段中那么它应该是
progressBar =(ProgressBar)v.findViewById(R.id.progressBar);
答案 1 :(得分:0)
将你的get活动()。findviewbyid()替换为v.findviewbyid();
将你的get活动()。findviewbyid()替换为v.findviewbyid();
将你的get活动()。findviewbyid()替换为v.findviewbyid();
将你的get活动()。findviewbyid()替换为v.findviewbyid();