分享按钮在安卓中不起作用



我有ListView,在其BaseAdapter中,我希望有一个弹出菜单,其中一个项目是"共享"项目,因此当用户单击它时,将弹出一个共享窗口/对话框:

这是我的弹出菜单是列表视图的基本适配器:

mViewHolder.optionMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(context, mViewHolder.optionMenuButton);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.share_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT).show();
if (item.getTitle() == "share") {
if (null == mainActivity) {
mainActivity = (MainActivity) context;
}
mainActivity.shareAction();
return true;
}
return false;
}
});
popup.show();//showing popup menu
}
});

这就是我尝试打开共享窗口/对话框的方式,它没有打开共享窗口/对话框,但显示我单击弹出菜单的"共享"项的吐司:

public void shareAction() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "You have to check this out: " + "https://www.google.com/";
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Check this out");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

ShareAction(( 方法位于 mainActivity 中,适配器是 ListView 的 BaseAdapter,此 ListView 位于 MainActivity 的一个片段中。

我也尝试了我创建的 ShareAction(( 方法的这个执行代码,它在没有片段的活动中工作得很好,这就是为什么它在这里不起作用很奇怪......

从编码中删除此代码,

if (null == mainActivity) {
mainActivity = (MainActivity) context;
}

并匹配这样的条件,

if (item.getTitle().equals( "share")) {
mainActivity.shareAction();
return true;
}

试试这个来显示共享对话框

public void shareApp(Context context)
{
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out yourAppName at: https://play.google.com/store/apps/details?id=" + appPackageName + refercode);//If you have refer code you can give
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, "Share with..."));
}

最新更新