2015-05-03 25 views
-1

我想用三個按鈕創建一個AlertDialog。其中兩個工作良好,但不是第三個。最後一個按鈕是SharedIntent並共享用戶的分數。我真的不知道是什麼原因導致飛機墜毀所以這裏是我的代碼:AlertDialog中的SharedIntent

alert.setNeutralButton("Partager", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
     sharingIntent.setType("text/plain"); 
     String shareBody = "text to share"; 
     sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "text"); 
     sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
     startActivity(Intent.createChooser(sharingIntent, "Share via"));     
    } 
}); 
+1

在您的文章中發佈logcat –

+0

您確定代碼在此方法中崩潰嗎? – RafaelC

+0

是的,我確定該應用程序運行良好,直到我嘗試分享。 我正在使用模擬器,所以當應用程序嘗試連接到Internet時它總是崩潰:LogCat上的錯誤是AndroidRuntimeException,但我也試過在手機上,它崩潰了,我不能告訴你爲什麼。 – Verratti

回答

0

我找到了解決辦法,這個問題是從代碼的最後一行,所以這裏是正確的代碼:

alert.setNeutralButton("Partager", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain"); 
    String shareBody = "text to share"; 
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "text"); 
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
    MyActivity.startActivity(Intent.createChooser(sharingIntent, "Share via"));     
    } 
}); 

並由您的「MyActivity」取代。