intent.putExtra()OS 4.0上的错误 - 如何解决?

时间:2018-02-13 23:46:25

标签: java android webview

我试图通过一个包含putExtra的按钮开始活动。但是应用程序停止处理按钮,我试图禁用 android:hardwareAccelerated =&#34; false&#34; < / strong>在Manifest中,但这不适用于Android 4.0,有人可以帮助我吗?

但显示错误

E/dalvikvm: VM aborting A/libc: Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 14318

MainActivity 中的代码:

  public void Button (View view){
    Intent intent = new Intent(MainActivity.this, Site.class);
    intent.putExtra("url", "https://example.com/");
    startActivity(intent); }

Site.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;


public class Site extends Activity {
    private static final String TAG = Site.class.getSimpleName();


    protected WebView mainWebView;
    private Context mContext;
    private WebView mWebviewPop;
    private FrameLayout mContainer;


    private String url = "http://example.com";// 


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

        mContainer = (FrameLayout) findViewById(R.id.container);
        mContext = this.getApplicationContext();

        clearCookies(this);

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);


        mainWebView = getWebView(mContext);


        mainWebView.setWebChromeClient(new MyCustomChromeClient());

        mainWebView.loadUrl(url);

        mContainer.addView(mainWebView);


    }



    WebView getWebView(Context context) {
        WebView wv = new WebView(context);
        wv.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        wv.clearCache(true);


        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setSupportMultipleWindows(true);

        if (Build.VERSION.SDK_INT >= 21) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager.getInstance().setAcceptThirdPartyCookies(wv, true);
        }


        return wv;
    }


    private class MyCustomChromeClient extends WebChromeClient {

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                                      boolean isUserGesture, Message resultMsg) {
            mWebviewPop = getWebView(mContext);
            mWebviewPop.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    String host = Uri.parse(url).getHost();
                    Log.d(TAG, "host=" + host);
                    if (host.contains("facebook.com")) {
                        view.loadUrl(url);
                        return true;
                    }

                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return false;
                }
            });
            mWebviewPop.setWebChromeClient(new MyCustomChromeClient());

            mContainer.addView(mWebviewPop);

            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(mWebviewPop);
            resultMsg.sendToTarget();

            return true;
        }

        @Override
        public void onCloseWindow(WebView window) {

            Log.d("onCloseWindow", "called");

            mWebviewPop.setVisibility(View.GONE);
            mContainer.removeView(mWebviewPop);
            mWebviewPop = null;

        }
    }

    @SuppressWarnings("deprecation")
    public static void clearCookies(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            Log.d(TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
            Log.d(TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
}

activity_site:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

1 个答案:

答案 0 :(得分:2)

在您的代码( Site.java )中,更改

activity_blog.xml

activity_site.xml

您的布局文件的名称。然后注释掉以下行:

mainWebView.setWebChromeClient(new MyCustomChromeClient());

在您的 Site.java 中,因为这会在我的测试中引发错误。稍后您可以决定使用之前的代码来处理它。有了这个,你的项目就在apis&gt; = 14上运行。祝你好运。