我希望在应用程序连接到互联网时显示正常的webview(实际上这就是我拥有并想要添加第二个片段),但是当它没有连接到互联网时(它有效,但它显示网页不可用,默认android浏览器,我不想要它),我想显示另一个带文本视图的片段和按钮TRY AGAIN。有人可以帮助我吗?我正在使用SherlockFragment,下面我使用Webview发布代码而没有另一个片段。谢谢你的回答。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout, container, false);
if (isOnline()){
WebView myWebView = (WebView) v.findViewById(R.id.id);
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Prosím čakajte. Prebieha načítavanie...");
progressDialog.setCancelable(true);
progressDialog.setProgress(0);
progressDialog.show();
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progressDialog != null)
progressDialog.setProgress(0);
getActivity().setProgress(progress * 1000);
progressDialog.incrementProgressBy(progress);
if (progress == 100 && progressDialog.isShowing())
progressDialog.dismiss();
}
});
myWebView.getSettings().setBuiltInZoomControls(false);
myWebView.getSettings().setSupportZoom(false);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("webpage");
}
else{
Intent intent = new Intent(getActivity(), class.class);
getActivity().startActivity(intent);
}
return v;
}
private boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
Log.v(getTag(), "Internet Connection Not Present");
return false;
}
}
}
LOG CAT
12-08 09:17:57.852: E/AndroidRuntime(17630): FATAL EXCEPTION: main
12-08 09:17:57.852: E/AndroidRuntime(17630): java.lang.NullPointerException
12-08 09:17:57.852: E/AndroidRuntime(17630): at sk.soszm.polytechnicka.Novinky.isOnline(Novinky.java:71)
12-08 09:17:57.852: E/AndroidRuntime(17630): at sk.soszm.polytechnicka.Novinky.onCreateView(Novinky.java:31)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.os.Handler.handleCallback(Handler.java:725)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.os.Looper.loop(Looper.java:137)
12-08 09:17:57.852: E/AndroidRuntime(17630): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-08 09:17:57.852: E/AndroidRuntime(17630): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 09:17:57.852: E/AndroidRuntime(17630): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 09:17:57.852: E/AndroidRuntime(17630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 09:17:57.852: E/AndroidRuntime(17630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 09:17:57.852: E/AndroidRuntime(17630): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您可以监控设备是否已连接到Internet。如果未连接,只需将WebView
的可见性设为GONE
并显示短信。
使用此选项检查互联网连接:
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
来源:http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
获得连接状态后,请执行以下操作:
if(isConnected) {
//your WebView code
} else {
//TextView showing "Try Again"
}
您可以使用setVisibility(int)
以编程方式显示/隐藏视图
来源:http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29