Bri*_*nte 3 java android webview
我正在使用以下代码在我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果历史存在而不是仅退出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 {
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();
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决一下吗?
得到它了!你在这一行的问题
WebView webview = new WebView(this);
Run Code Online (Sandbox Code Playgroud)
您正在函数内部创建变量,而不是使用您的成员变量,因此您的成员变量在onKeyDown函数内为null.
只需更换它
webview = new WebView(this);
Run Code Online (Sandbox Code Playgroud)