片段上的Android WebView - Chrome已停止

时间:2018-04-20 11:30:13

标签: android android-webview

我正在尝试开发Android应用。其中一部分需要WebView才能显示Google Charts中的Treemap图表。

我遵循了许多指南来实现WebView(这并不太难),在这之后,当我访问WebView所在的Fragment时,屏幕上会显示一条消息,告诉我:Chrome已停止。 / p>

Screenshot

我的代码是下一个。

Layout.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=".GearProductsFragment">

    <!-- TODO: Update blank fragment layout -->


    <WebView
        android:id="@+id/webview"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我在Manifest中获得了互联网许可。

片段的Java类是下一个:

WebView mWebView;
private String url = "https://www.google.com";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview = inflater.inflate(R.layout.fragment_gear_products, container, false);

    mWebView = (WebView) rootview.findViewById(R.id.webview);

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.loadUrl(url);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    return rootview;
}

我已经尝试再次打开Chrome,就像屏幕截图中所说的那样,但屏幕保持白色。

logcat的

W/chromium: [WARNING:ipc_message_attachment_set.cc(49)] MessageAttachmentSet destroyed with unconsumed attachments: 
0/1 W/zygote: Attempt to remove non-JNI local reference, dumping thread 
W/cr_CrashFileManager:/data/user/0/com.pharmamodelling.crosssalesmockup/cache/WebView/Crash Reports does not exist or is not a directory 

2 个答案:

答案 0 :(得分:0)

嘿,你可以尝试这段代码:

 public class MainActivity extends AppCompatActivity
    implements  SwipeRefreshLayout.OnRefreshListener {

private static final String TAG = "MainActivity";
private WebView webView;
private SwipeRefreshLayout mSwipe;
private ProgressDialog progressDialog;
private static final String url = "https://stackoverflow.com/";
//private Handler handler;
private static int backCount;
private String message = "If you want to close the app. Please tap again.";

private boolean loadingFinished = true;
private boolean redirect = false;
private static final int MY_PERMISSION_REQUEST_CODE = 123;
private boolean mShouldPause = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.app_bar_main);
    webView = (WebView) findViewById(R.id.webview);
    mSwipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
    mSwipe.setOnRefreshListener(this);
    backCount = 1;
   // handler = new Handler();
    loadUrlInWebView(url);

}

private void loadUrlInWebView(String url) {

    Log.i(TAG, "load in webview");
    if (webView == null) {
        Log.e(TAG, "webview is null");
        return;
    }

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.setInitialScale(1);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!loadingFinished) {
                redirect = true;
            }

            loadingFinished = false;
            if( URLUtil.isNetworkUrl(url) ) {

                view.loadUrl(url);
                if (url.contains("youtube.com")) {
                    mShouldPause = true;
                }else if(url.contains(".pdf")|| url.contains(".xls")){
                    checkPermission();
                }else{
                    mShouldPause = true;
                }
                super.doUpdateVisitedHistory(view, url, true);
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            loadingFinished = false;
            showProgressBar();
            //SHOW LOADING IF IT ISNT ALREADY VISIBLE
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mSwipe.setRefreshing(false);
            if(!redirect){
                loadingFinished = true;
            }

            if(loadingFinished && !redirect){
                //HIDE LOADING IT HAS FINISHED
                hideProgressBar();
            } else{
                redirect = false;
            }
            try {

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Toast.makeText(MainActivity.this, "Unable to load the page", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request,
                                        WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            Toast.makeText(MainActivity.this, "Unable to load the page", Toast.LENGTH_SHORT).show();
        }
    });

    webView.loadUrl(url);
}

private void startDownLoad() {
    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDescription,
                                    String mimetype, long contentLength) {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setMimeType(mimetype);
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie",cookies);
            request.addRequestHeader("user-Agent",userAgent);
            request.setDescription("Downloading file...");
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            String fileName = URLUtil.guessFileName(url,contentDescription,mimetype);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,fileName);

            DownloadManager dManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dManager.enqueue(request);
            Toast.makeText(MainActivity.this, "DownLoading File", Toast.LENGTH_SHORT).show();
        }
    });

}
protected void checkPermission(){
    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
        if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                // show an alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Write external storage permission is required.");
                builder.setTitle("Please grant permission");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ActivityCompat.requestPermissions(
                                MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSION_REQUEST_CODE
                        );
                    }
                });
                builder.setNeutralButton("Cancel",null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }else {
                // Request permission
                ActivityCompat.requestPermissions(
                        MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSION_REQUEST_CODE
                );
            }
        }else {
            startDownLoad();
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    switch(requestCode){
        case MY_PERMISSION_REQUEST_CODE:{
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
               startDownLoad();
            }else {
                // Permission denied
            }
        }
    }
}
private void hideProgressBar() {
    if (null != progressDialog && progressDialog.isShowing()) {
        progressDialog.dismiss();
        progressDialog = null;
    }
}

private void showProgressBar() {
    if (progressDialog == null){
        progressDialog = new ProgressDialog(MainActivity.this);
    }
    progressDialog.setMessage("Loading...");
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
public void onBackPressed() {
    //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (null != progressDialog && progressDialog.isShowing()) {
        progressDialog.dismiss();
        progressDialog = null;
    }
    if (webView.canGoBack()) {
        webView.goBack();
        return;
    }
    if(backCount > 0){
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        backCount =0;
        return;
    }


    super.onBackPressed();
}
@Override
public void onPause() {
    super.onPause();
    if(mShouldPause){
        webView.onPause();
    }
    mShouldPause = false;
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
protected void onDestroy() {
    super.onDestroy();
    mSwipe.setOnRefreshListener(null);
}

@Override
public void onRefresh() {
    mSwipe.setRefreshing(true);
    webView.reload();
}

}

答案 1 :(得分:0)

发现错误。当我在模拟器上启动应用程序(我使用的是Pixel C或Pixel 2)时,WebView无法正常工作,Chrome每次都会停止。 但是当我在真实设备上启动应用程序时,WebView工作正常。我没有改变任何代码,它也是一样的。