2012-03-27 52 views
0

我試圖在Facebook上分享內容創建AlertDialog錯誤在我的分享按鈕點擊時一個onClick

我麻煩一個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(); 

但我的web視圖在其他活動開身,這不是我想要什麼

回答

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; 
    } 
}); 
+0

我已經得到了同樣的效果比我的第二次嘗試,我的webview內置在其他活動 – 2012-03-27 17:40:10

+0

工程就像一個魅力!非常感謝你 – 2012-03-27 17:58:41

1

設法改變ContextgetBaseContext()提供給AlertDialogYourActivityName.this

+0

錯誤變化: 從活動上下文之外調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。這真的是你想要的嗎? – 2012-03-27 17:25:48

+0

@RenaudFavier嗯,我不知道你在做什麼,所以把這個標誌添加到你在'startActivity()'中使用的'Intent'中。 – Luksprog 2012-03-27 17:28:46

+0

不,我不想嘗試啓動任何活動,我只是不想在我的alertdialog中顯示一個webview,我沒有做任何事情比我向你展示的東西 – 2012-03-27 17:33:00

相關問題