Renaud Favier
Renaud Favier

Reputation: 401

Error creating an AlertDialog in an onClick

I'm trying to share a content on facebook when clicking on my share button

i've trouble creating an alert dialog in an 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();

            }
        });

here is my 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

here is my second try :

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();

but my webView open itself in an other activity, that's not what i want

Upvotes: 0

Views: 268

Answers (2)

iForests
iForests

Reputation: 6949

Try it:

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

    (... some code here...)

builder.show();

And you have to setWebViewClient like this:

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

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

Upvotes: 1

user
user

Reputation: 87064

Try to change the Context you supply to the AlertDialog from getBaseContext() to YourActivityName.this.

Upvotes: 1

Related Questions