java–Android启用webview中的后退按钮

java–Android启用webview中的后退按钮,第1张

概述我正在使用以下代码在我的Android应用中显示webview.packagecom.company.myapp;importcom.google.android.apps.analytics.GoogleAnalyticsTracker;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.Window;import

我正在使用以下代码在我的Android应用中显示webvIEw.

package com.company.myapp;import com.Google.androID.apps.analytics.GoogleAnalyticsTracker;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.Window;import androID.webkit.WebChromeClIEnt;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.Widget.Toast;public class ArticlesActivity extends Activity {    /** Initialize the Google Analytics Tracker */    GoogleAnalyticsTracker tracker;    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getwindow().requestFeature(Window.FEATURE_PROGRESS);        getwindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBIliTY_ON);        WebVIEw webvIEw = new WebVIEw(this);        setContentVIEw(webvIEw);         setProgressbarVisibility(true);         webvIEw.getSettings().setJavaScriptEnabled(true);        webvIEw.setScrollbarStyle(VIEw.SCRolLbarS_INSIDE_OVERLAY);        final Activity activity = this;        tracker = GoogleAnalyticsTracker.getInstance();        // Start the tracker, updating Google every 20 seconds        tracker.start((String) getText(R.string.analyticsID), 20, this);        webvIEw.setWebChromeClIEnt(new WebChromeClIEnt() {           public voID onProgressChanged(WebVIEw vIEw, int progress) {             activity.setProgress(progress * 100 );           }         });         webvIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() {             public voID onReceivedError(WebVIEw vIEw, int errorCode, String description, String failingUrl) {                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();            }        });        webvIEw.loadUrl("http://www.Google.com");    }    @OverrIDe    public voID onResume() {        tracker.trackPageVIEw("ArticlesActivity");        super.onResume();    }    @OverrIDe    protected voID onDestroy() {      super.onDestroy();      // Stop the tracker when it is no longer needed.      tracker.stop();    }}

如果历史存在而不是仅退出webvIEw,我需要启用后退按钮以退回.

I’ve tried many different code examples such as this但无法上班.按下后退按钮时,应用程序才会关闭.

这是我的代码与后退按钮代码,但它只是在按下后退按钮时崩溃应用程序:

package com.company.myapp;import com.Google.androID.apps.analytics.GoogleAnalyticsTracker;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.vIEw.Window;import androID.webkit.WebChromeClIEnt;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.Widget.Toast;public class ArticlesActivity extends Activity {    WebVIEw webvIEw;    /** Initialize the Google Analytics Tracker */    GoogleAnalyticsTracker tracker;    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        getwindow().requestFeature(Window.FEATURE_PROGRESS);        getwindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBIliTY_ON);        WebVIEw webvIEw = new WebVIEw(this);        setContentVIEw(webvIEw);         setProgressbarVisibility(true);         webvIEw.getSettings().setJavaScriptEnabled(true);        webvIEw.setScrollbarStyle(VIEw.SCRolLbarS_INSIDE_OVERLAY);        final Activity activity = this;        tracker = GoogleAnalyticsTracker.getInstance();        // Start the tracker, updating Google every 20 seconds        tracker.start((String) getText(R.string.analyticsID), 20, this);        webvIEw.setWebChromeClIEnt(new WebChromeClIEnt() {           public voID onProgressChanged(WebVIEw vIEw, int progress) {             activity.setProgress(progress * 100 );           }         });         webvIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() {             public voID onReceivedError(WebVIEw vIEw, int errorCode, String description, String failingUrl) {                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();            }        });        webvIEw.loadUrl("http://www.Google.com");    }    @OverrIDe    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (keyCode == KeyEvent.KEYCODE_BACK) {            webvIEw.goBack();            return true;        }        return super.onKeyDown(keyCode, event);    }    @OverrIDe    public voID onResume() {        tracker.trackPageVIEw("ArticlesActivity");        super.onResume();    }    @OverrIDe    protected voID onDestroy() {      super.onDestroy();      // Stop the tracker when it is no longer needed.      tracker.stop();    }}

有人可以帮我解决一下吗?

解决方法:

得到它了!你在这一行的问题

WebVIEw webvIEw = new WebVIEw(this);

您正在函数内部创建变量,而不是使用您的成员变量,因此您的成员变量在onKeyDown函数内为null.

只需更换它

webvIEw = new WebVIEw(this);
总结

以上是内存溢出为你收集整理的java – Android启用webview中的后退按钮全部内容,希望文章能够帮你解决java – Android启用webview中的后退按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112718.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存