Android 如何将ShareActionProvider添加到OnOptions ItemSelected
我是Android新手。我想在ActionBar的一个菜单中添加共享功能。actionbar中有两个菜单,分别是Share和InfoAndroid 如何将ShareActionProvider添加到OnOptions ItemSelected,android,share,Android,Share,我是Android新手。我想在ActionBar的一个菜单中添加共享功能。actionbar中有两个菜单,分别是Share和Info @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up butto
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.share) {
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
doShare();
return true;
}
return super.onOptionsItemSelected(item);
}
private void doShare() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"Selected text");
mShareActionProvider.setShareIntent(intent);
}
在我添加代码行并运行应用程序之后。它返回错误Java空指针异常
FATAL EXCEPTION: main
Process: com.example, PID: 1222
java.lang.NullPointerException at com.example.MainActivity.doShare(MainActivity.java:94)
at com.example.MainActivity.onOptionsItemSelected(MainActivity.java:83)
at android.app.Activity.onMenuItemSelected(Activity.java:2617)
我搜索了一下,发现Android中的大部分教程都在教如何在OnCreateOptions菜单中添加ShareActionProvider,但我在操作栏中有两个菜单。如何从Actionbar中的选定菜单共享意图 试试这个:
doShare(mShareActionProvider)
...
private void doShare(ShareActionProvider mShareActionProvider) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"Selected text");
mShareActionProvider.setShareIntent(intent);
}
通过在OnCreateOptions菜单中设置ShareActionProvider的新实例,我可以使用它。我们使用setActionProvider而不是getActionProvider函数来共享意图
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.share);
share = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(item, share);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.share) {
onShareAction();
return true;
}
return super.onOptionsItemSelected(item);
}
private void onShareAction() {
String yourShareText = "test";
Intent shareIntent = ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
if (share != null) {
share.setShareIntent(shareIntent);
}
}
张贴你的堆栈trace@RuchirBaronia,我已经添加了痕迹,谢谢!你的第83行是什么@Micheletbs使该方法公开,并且tryits也不工作,当我调试时,它返回mShareActionProvider=null。为什么?