我正在测试我的应用,现在我已经从我上一篇文章中更新了它们。当我按下设备上的后退按钮时,应用程序卡住并且屏幕上出现错误。我无法按下模拟器上的按钮,所以我无法给出错误。
我还希望当我点击没有 http:// 的链接时,它会在网页浏览中打开,当网址为 http:// 时,它会打开在浏览器中。
问候Christophe
main.java
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import be.my.app.ConnectionDetector;
public class MainActivity extends Activity {
@SuppressLint("SetJavaScriptEnabled")
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*=== instellingen ===*/
String url = "http://app.site.be?av=1"; //starturl
/*=== verwerkingdsdata ===*/
WebView view = (WebView) this.findViewById(R.id.webView1); //koppeling layout aan id
view.getSettings().setJavaScriptEnabled(true); // javascript aan
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
view.loadUrl(url); // laad de url
} else {
setContentView(R.layout.niet_verbonden); //geen verbinding error weergeven
}
view.setWebViewClient(new MyWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack() == true){
myWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("http://")){ // Could be cleverer and use a regex
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
} else {
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
}
答案 0 :(得分:0)
if(myWebView.canGoBack() == true){
这里你引用myWebView
由于没有设置它总是为空,
以后你有
WebView view = (WebView) this.findViewById(R.id.webView1);
将其替换为
myWebView = (WebView) this.findViewById(R.id.webView1);
这肯定是问题
希望这会有所帮助
编辑: 用这个替换你的代码:
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import be.my.app.ConnectionDetector;
public class MainActivity extends Activity {
@SuppressLint("SetJavaScriptEnabled")
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*=== instellingen ===*/
String url = "http://app.site.be?av=1"; //starturl
/*=== verwerkingdsdata ===*/
myWebView= (WebView) this.findViewById(R.id.webView1); //koppeling layout aan id
myWebView.getSettings().setJavaScriptEnabled(true); // javascript aan
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
myWebView.loadUrl(url); // laad de url
} else {
setContentView(R.layout.niet_verbonden); //geen verbinding error weergeven
}
myWebView.setWebViewClient(new MyWebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(myWebView.canGoBack() == true){
myWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("http://")){ // Could be cleverer and use a regex
return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
} else {
view.loadUrl(url); // Stay within this webview and load url
return true;
}
}
}