webview里面的onclick imagebutton错误

时间:2015-03-19 20:52:19

标签: android webview imagebutton

我正在尝试使用图像按钮打开webview。我能够用意图和uri.parse这样做:

        twbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
            Uri.parse("website"));
            startActivity(browserIntent);

但它会退出应用程序并打开浏览器。我希望它在不离开应用程序的情况下打开

    fbbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
            WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.setWebViewClient(new WebViewClient());
            myWebView.loadUrl("website");


        }
    });

但是我收到了错误:

W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
W/AwContents﹕ onDetachedFromWindow called when already detached. Ignoring

2 个答案:

答案 0 :(得分:0)

您确定在布局webview中有元素fragment_socialmedia.xml吗?

 View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
 WebView myWebView = (WebView) rootView.findViewById(R.id.webview);

这就是你得到这个例外的原因:android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference

答案 1 :(得分:0)

你需要交换这两行:

myWebView.loadUrl("website");
myWebView.setWebViewClient(new WebViewClient());

在执行加载之前设置WebViewClient

myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("website");