gpt4 book ai didi

android - 在 WebView 中启用后退按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:54 26 4
gpt4 key购买 nike

我刚刚开始使用 Android SDK。我想用 WebView 制作一个仅显示本地网页的简单应用程序。我使用了在网上找到的示例来制作它。

该应用程序运行得很好,直到我想添加一个后退按钮。当按下手机后退按钮时,应用程序崩溃。

这是我的 MainActivity.java

package com.hspd.avhor;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

WebView wv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView)findViewById(R.id.hspd_webview);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/one.htm");
}

@Override
public void onBackPressed()
{
if(wv.canGoBack())
wv.goBack();
else
super.onBackPressed();
}

@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;
}

}

这是我的activity_main.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
android:id="@+id/hspd_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</ScrollView>

这是我在 logcat 中的错误:

04-22 17:48:06.953: W/dalvikvm(799): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-22 17:48:06.972: E/AndroidRuntime(799): FATAL EXCEPTION: main
04-22 17:48:06.972: E/AndroidRuntime(799): java.lang.NullPointerException
04-22 17:48:06.972: E/AndroidRuntime(799): at com.hspd.avhor.MainActivity.onKeyDown(MainActivity.java:41)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.os.Looper.loop(Looper.java:137)
04-22 17:48:06.972: E/AndroidRuntime(799): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-22 17:48:06.972: E/AndroidRuntime(799): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 17:48:06.972: E/AndroidRuntime(799): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 17:48:06.972: E/AndroidRuntime(799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 17:48:06.972: E/AndroidRuntime(799): at dalvik.system.NativeStart.main(Native Method)

提前致谢:)

最佳答案

覆盖 onKeyDown(params),检查按下的键是否为后退按钮,检查 webview 是否可以导航到上一页,如果是则导航到上一页。如果没有网页显示,您可以完成 Activity

你可以使用下面的

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
//if Back key pressed and webview can navigate to previous page
webView.goBack();
// go back to previous page
return true;
}
else
{
finish();
// finish the activity
}
return super.onKeyDown(keyCode, event);
}

编辑:

删除 xml 布局中的 ScrollView 。试试下面的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/wv"></WebView>
</LinearLayout>

编辑:2

这是一个有效的示例。在三星 galaxy s3 上测试

activtiy_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/wv"/>

</RelativeLayout>

主要 Activity

 public class MainActivity extends Activity {
private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MainActivity.this.setProgress(progress * 1000);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});

webView.loadUrl("http://slashdot.org/");

}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}

关于android - 在 WebView 中启用后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16152491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com