我想在我的应用程序使用android.support.v7.widget.ShareActionProvider的动作条。所以我也跟着从Android电子文档中的例子,但有一些问题。 这是我的菜单的xml:
I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues. Here's my menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@drawable/ic_action_share"
android:title="@string/action_share"
myapp:showAsAction="ifRoom"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
这是我的code创建共享操作按钮:
here's my code to create the share action button:
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.share, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
super.onCreateOptionsMenu(menu, inflater);
}
我的问题是:
My question is:
MenuItemCompat.getActionProvider(shareItem)始终返回我空,为什么? 当我评论的线条,分享按钮会出现在酒吧,但不执行任何操作,而点击,如何解决它(如果问题1解决不了)?顺便说一句,我检查$ C $ MenuItemCompat.getActionProvider的CS,它看起来像这种方法将检查菜单项实现SupportMenuItem界面并返回失败,如果事实并非如此。我怎么能解决呢?
btw, I checked codes of MenuItemCompat.getActionProvider, it looks like this method will check if the menu item implements SupportMenuItem interface and returns fail if it isn't. How could I deal with it?
下面这个是工程,使ShareActionProvider NOT NULL唯一的解决办法...我使用设置ActionProvider,而不是...请参见下面的code:
Here this is the only solution that works to make ShareActionProvider not null...i use set ActionProvider instead...see the code below:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.messages_activity_menu, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
shareActionProvider = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(menuItem, shareActionProvider);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_item_share){
onShareAction();
}
return super.onOptionsItemSelected(item);
}
private void onShareAction(){
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" + getPackageName();
String yourShareText = getResources().getString(R.string.share_text) + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
if (shareActionProvider != null) {
shareActionProvider.setShareIntent(shareIntent);
}
}
和... XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:icon="@drawable/ic_action_share"
android:showAsAction="ifRoom|withText"
android:title="@string/menu_item_share" />
</menu>
和可检查其它东西:
该活动已扩展ActionBarActivity:
the activity has to extends ActionBarActivity:
MyActivity extends ActionBarActivity
检查并使用进口:
check and use this imports:
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
在AndroidManifest.xml中把这个线在活动的标签属性:
In AndroidManifest.xml put this line in your activity's tag attributes:
android:theme="@style/Theme.AppCompat.Light"
如果你不知道如何导入V7和V4兼容库看到:http://developer.android.com/tools/support-library/setup.html
If you dont know how to import v7 and v4 compatibility libraries see: http://developer.android.com/tools/support-library/setup.html