Android Webview Back按钮适用于模拟器,但不适用于设备

青春须早为,岂能长少年。这篇文章主要讲述Android Webview Back按钮适用于模拟器,但不适用于设备相关的知识,希望能为你提供帮助。
我有以下用于android javaWebview代码。后面的button在Android Studio模拟器中正常工作但是当我在任何Android设备上安装相同的apk时它不再起作用了。点击后面的button只会让移动设备上的应用程序崩溃。有人可以帮忙吗?

import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.webkit.ValueCallback; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class MainActivity extends AppCompatActivity{public static final String PAGE_URL= "www.domain.com"; WebView webb; private static final int INPUT_FILE_REQUEST_CODE = 1; private static final int FILECHOOSER_RESULTCODE = 1; private static final String TAG = MainActivity.class.getSimpleName(); private WebSettings webSettings; private ValueCallback< Uri> mUploadMessage; private Uri mCapturedImageURI = null; private ValueCallback< Uri[]> mFilePathCallback; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webb = (WebView) findViewById(R.id.webb1); webSettings = webb.getSettings(); webSettings.setjavascriptEnabled(true); webSettings.setLoadWithOverviewMode(true); webSettings.setAllowFileAccess(true); //webb.setWebViewClient(new WebViewClient()); webb.setWebViewClient(new Client()); if (Build.VERSION.SDK_INT > = 19) { webb.setLayerType(View.LAYER_TYPE_HARDWARE, null); } else if (Build.VERSION.SDK_INT > = 11 & & Build.VERSION.SDK_INT < 19) { webb.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } webb.loadUrl(PAGE_URL); }@Override public void onActivityResult(int requestCode, int resultCode, Intentdata) { if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) { if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { super.onActivityResult(requestCode, resultCode, data); return; } Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (data =https://www.songbingjia.com/android/= null) { if (mCameraPhotoPath != null) { results = new Uri[]{Uri.parse(mCameraPhotoPath)}; } } else { String dataString = data.getDataString(); if (dataString != null) { results = new Uri[]{Uri.parse(dataString)}; } } } mFilePathCallback.onReceiveValue(results); mFilePathCallback = null; } else if (Build.VERSION.SDK_INT < = Build.VERSION_CODES.KITKAT) { if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) { super.onActivityResult(requestCode, resultCode, data); return; } if (requestCode == FILECHOOSER_RESULTCODE) { if (null == this.mUploadMessage) { return; } Uri result = null; try { if (resultCode != RESULT_OK) { result = null; } else { result = data == null ? mCapturedImageURI : data.getData(); } } catch (Exception e) { Toast.makeText(getApplicationContext(),"activity :" + e, Toast.LENGTH_LONG).show(); } mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } return; }@Override public void onBackPressed() { webb = (WebView) findViewById(R.id.webb1); if (webb.canGoBack()) { webb.goBack(); } else { super.onBackPressed(); } }public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) & & webb != null & & webb.canGoBack()) { webb.goBack(); return false; } return super.onKeyDown(keyCode, event); }public class Client extends WebViewClient { ProgressDialog progressDialog; public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains("mailto:")) { view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } else if (url.contains("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); view.reload(); return true; } else { view.loadUrl(url); return true; } }public void onPageFinished(WebView view, String url) { try { if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } } catch (Exception exception) { exception.printStackTrace(); } } } }

谢谢
答案请在onBackPressed活动上进行以下代码
if (webb.canGoBack()) { webb.goBack(); } else { finish(); }

另一答案【Android Webview Back按钮适用于模拟器,但不适用于设备】以下是android studio中的日志:
W/zygote: Attempt to remove non-JNI local reference, dumping thread D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) I/zygote: NativeAlloc concurrent copying GC freed 0(44KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2MB/4MB, paused 27us total 109.624ms W/zygote: Attempt to remove non-JNI local reference, dumping thread W/zygote: Attempt to remove non-JNI local reference, dumping thread W/zygote: Attempt to remove non-JNI local reference, dumping thread I/zygote: NativeAlloc concurrent copying GC freed 784(86KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1973KB/3MB, paused 21us total 113.240ms D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) W/zygote: Attempt to remove non-JNI local reference, dumping thread D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) I/zygote: NativeAlloc concurrent copying GC freed 3435(216KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1789KB/3MB, paused 52us total 118.789ms D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) I/zygote: Do full code cache collection, code=199KB, data=https://www.songbingjia.com/android/149KB I/zygote: After code cache collection, code=197KB, data=114KB W/zygote: Attempt to remove non-JNI local reference, dumping thread D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300) I/zygote: NativeAlloc concurrent copying GC freed 1118(111KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 1844KB/3MB, paused 45us total 142.205ms D/EGL_emulation: eglMakeCurrent: 0xa297f160: ver 2 0 (tinfo 0x91cba300)


    推荐阅读