【问题标题】:Handling Back Button in Android Webview处理 Android Webview 中的后退按钮
【发布时间】:2017-09-04 22:28:49
【问题描述】:

我使用 WebView 和 Html5 开发了一个应用程序,它可以工作,单击网页中的链接会转到我应用程序内 html 文件中的下一页。但是当我点击手机的后退按钮时,它给了我这个错误:不幸的是,APPNAME 已停止。我想返回网站的上一页。

网站建立:https://github.com/01org/appframework

MainActivity.java

package com.package.name;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    private WebView WebView;

    //Back Button Code

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (WebView.canGoBack()) {
                        WebView.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

//--------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView=(WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(true);

        webView.loadUrl("file:///android_asset/index.html");
    }

}

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.package.name.MainActivity">

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </WebView>

</android.support.constraint.ConstraintLayout>

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package.name">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

【问题讨论】:

  • 在这里发布你的日志
  • 纳文古普塔,它是空的。
  • 哥们,你的代码对我来说很好用。
  • 怎么样,我正在使用 Android Studio。

标签: android html webview


【解决方案1】:

首先,
检查 WebView 是否为空.. 将全局变量重命名为 webView (private WebView webView;) 并在 onCreate 中将 WebView webView=(WebView) findViewById(R.id.webview); 更改为 webView=(WebView) findViewById(R.id.webview);

您也可以尝试将后退按钮处理代码移至

@Override
public void onBackPressed()
{
 ....
}

而不是

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    ...
} 

如:

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

https://developer.android.com/training/implementing-navigation/temporal.html#back-webviews中所述

【讨论】:

  • 检查 WebView 是否为空.. 将全局变量重命名为 webView (private WebView webView;) 并在 onCreate 中将 WebView webView=(WebView) findViewById(R.id.webview); 更改为 webView=(WebView) findViewById(R.id.webview); - 将其添加到答案中
  • 就是这样,你拯救了我的一天。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2011-10-26
  • 2011-04-02
  • 2017-06-20
相关资源
最近更新 更多