2013-08-19 34 views
3

我有問題。我按照很多指南,但仍無法讓我的「分享」按鈕正常工作。我在ActionBar中顯示了圖標,但是當我按下android:showAsAction =「always」時沒有任何事情發生。但是,當android:showAsAction =「從不」它有效。我只是想分享圖標始終顯示在ActionBar中。我做錯了什麼?請幫助在ActionBar中添加一個簡單的共享操作

這裏是我的代碼:

public class Main extends Activity { 

    private ShareActionProvider mShareActionProvider; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     MenuItem item = menu.findItem(R.id.shareButton); 
     mShareActionProvider = (ShareActionProvider) item.getActionProvider(); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.shareButton: 
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
      sharingIntent.setType("text/plain"); 
      String shareBody = "Check it out"; 
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject"); 
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
      startActivity(Intent.createChooser(sharingIntent, "Share via")); 
      return true; 
     case R.id.aboutButton: 
      Intent intent = new Intent(this, About.class); 
      startActivity(intent); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 
} 

這是我menu.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/shareButton" 
     android:actionProviderClass="android.widget.ShareActionProvider" 
     android:title="@string/share" 
     android:showAsAction="always"/> **---> when I put here "NEVER" then it works! But I want Share to be always as icon** 

    <item 
     android:id="@+id/aboutButton" 
     android:actionProviderClass="android.widget.ShareActionProvider" 
     android:showAsAction="never" 
     android:title="@string/about_dev"/> 
</menu> 

回答

1

嘗試 「意圖份額=新意圖(Intent.ACTION_SEND)」 代替。 你似乎在做一切正確!

7

如果didn't找到解決的辦法試試這個得到ShareActionProvider

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu, menu); 
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share) 
      .getActionProvider(); 
    mShareActionProvider.setShareIntent(doShare()); 
    return true; 
} 

而且doShare()是:

public Intent doShare() { 
    // populate the share intent with data 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("text/plain"); 
    intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want"); 
    return intent; 
} 
+0

按照這一辦法我必須知道的數據共享選項菜單時被建造。但是當我真正點擊按鈕時,只知道要共享哪些數據? – Martin