在onClick中创建AlertDialog时出错

时间:2012-03-27 17:19:18

标签: android alertdialog

点击我的分享按钮时,我正试图在Facebook上分享内容

我在onclick中创建警告对话框时遇到了麻烦,

((Button) findViewById(R.id.actuDetailsShareButton)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(ActuDetailActivity.this);
            builder.setTitle("Partagez sur facebook");
            WebView facebookWV = new WebView(ActuDetailActivity.this);
            LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
            facebookWV.setLayoutParams(lp);
            facebookWV.loadUrl("https://m.facebook.com/sharer.php?u=http://www.lalal.com");
            builder.setView(facebookWV);
            builder.show();

            }
        });

这是我的logcat:

03-27 19:18:23.007: E/AndroidRuntime(24773): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

这是我的第二次尝试:

Dialog dialog = new Dialog(ActuDetailActivity.this);
                dialog.setContentView(R.layout.share_alert_dialog_layout);
                dialog.setTitle("lolol");
                dialog.setCancelable(true);
                ((TextView) dialog.findViewById(R.id.shareAlertDialogTextView)).setText("share on facebook");
                ((WebView) dialog.findViewById(R.id.shareAlertDialogWebView)).loadUrl("https://m.facebook.com/sharer.php?u=http://www.lalal.com");
                dialog.show();

但是我的webView在其他活动中打开了自己,这不是我想要的

2 个答案:

答案 0 :(得分:1)

尝试将Context提供的AlertDialoggetBaseContext()更改为YourActivityName.this

答案 1 :(得分:1)

试一试:

AlertDialog.Builder builder = new AlertDialog.Builder(YouActivityName.this);

    (... some code here...)

builder.show();

你必须setWebViewClient这样:

WebView facebookWV = new WebView(YouActivityName.this);
facebookWV.getSettings().setJavaScriptEnabled(true);

facebookWV.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        return false;
    }
});