我在android中制作webview,我是android的初学者。在我的代码下面,我面临以下问题。
后退按钮不起作用,当我访问我的应用程序的不同页面并按回按钮然后它不起作用并给出错误应用程序已停止并在我的移动设备的主屏幕上收回我。
< / p>
我希望当任何用户打开这个应用程序然后如果互联网没有连接或不工作那么它给警报一些像“这个应用程序需要互联网连接”和默认情况下我的给定背景显示wihotu互联网连接。如果找到互联网连接,那么此应用程序正常工作。
我是android新手。提前我非常感谢这个帮助。请在你的回答中写下修复以下问题的代码。提前致谢
package com.example.edarabia;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.annotation.SuppressLint;
import android.view.KeyEvent;
@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
WebView mywebview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mywebview.setWebViewClient(new WebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mywebview.canGoBack()) {
mywebview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
您应该删除
@Override
public void onBackPressed (){
if(webView.canGoBack()) webView.goBack();
else super.onBackPressed();
}
避免NullPointerException。为什么要使用这样的实现?
2.您可以将此功能用于您的目的:
public boolean isOnline() {
boolean res = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
res= true;
}
else
{
Toast.makeText(context, "You've lost internet connection. Please check it and try again", Toast.LENGTH_LONG).show();
res = false;
}
return res;
}
并在活动中:
boolean isOnline = isOnline();
if (isOnline)
{
//What you want to do if there's a connection..
}
答案 1 :(得分:0)
您可以执行以下操作,
1.取消后退按钮功能
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
2.在应用启动时检查互联网连接,将以下代码放入onCreate()
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
Toast.makeText(this,
"Please check your wireless connection and try again.",
Toast.LENGTH_LONG).show();
}
// if positive, fetch the articles in background
else
// Do your task...
} else {
Toast.makeText(this,
"Please check your wireless connection and try again.",
Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
您尚未将主WebView webView;
初始化,而您在onBackPressed()
方法中指的是为什么它会给您错误。
只需在文件中进行如下更改:
public class MainActivity extends Activity { WebView mywebview; <<--- instead of webview use mywebview which you have declared in your onCreate. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mywebview = (WebView) findViewById(R.id.webView1); mywebview.loadUrl("http://www.grafdom.com/operations/projects/ma/edarabiaapp/"); WebSettings webSettings = mywebview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setBuiltInZoomControls(true); mywebview.setWebViewClient(new WebViewClient()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onBackPressed (){ if(mywebview.canGoBack()) mywebview.goBack(); else super.onBackPressed(); } }
第二个问题:
您可以使用以下方法检查您的互联网连接:
public static boolean IsNetConnected() { boolean m_netConnected = false; try { ConnectivityManager m_connectivity = (ConnectivityManager) AppConstants.m_context.getSystemService(Context.CONNECTIVITY_SERVICE); if (m_connectivity == null) { Log.w(TAG, "couldn't get connectivity manager"); m_netConnected = false; } else { NetworkInfo[] m_info = m_connectivity.getAllNetworkInfo(); if (m_info != null) { for (int i = 0; i < m_info.length; i++) { if (m_info[i].getState() == NetworkInfo.State.CONNECTED) { m_netConnected = true; } } } } } catch (Throwable e) { ErrorReporter.getInstance().handleException(e); if (AppConstants.DEBUG) { Log.e(TAG, "IsNetConnected Error" + e.toString(), e); } m_netConnected = false; } return m_netConnected; }
您可以使用以下方法:
if(!IsNetConnected()) { showDialog(MainActivity.this,"This application required internet connection"); //set your default background as per your requirements. }
答案 3 :(得分:0)
覆盖 onBackPressed()
的功能@Override
public void onBackPressed() {
finish();
}
希望这会对你有所帮助。