【问题标题】:Android share app dialog box display errorAndroid分享应用对话框显示错误
【发布时间】:2013-07-30 05:07:19
【问题描述】:

我在实现菜单以共享我的应用时遇到问题。当我打开带有图标 share_button 的活动时,会立即显示“共享方式”对话框。我认为我对这行代码有问题 "startActivity(Intent.createChooser(shareIntent(), "Share..."));"

这是我的代码

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mainpage, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);

    mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    mShareActionProvider.setShareHistoryFileName(null);
    // Create the share Intent
    String playStoreLink = "https://play.google.com/store/apps/details?id=" +
        getPackageName();
    String yourShareText = "Install this app " + playStoreLink;
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
        .setType("text/plain").setText(yourShareText).getIntent();
    // Set the share Intent
    mShareActionProvider.setShareIntent(shareIntent);
    startActivity(Intent.createChooser(shareIntent(), "Share via"));
    return true;
}

菜单项

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:title="Share"
    android:icon="@drawable/ic_share"
    android:actionProviderClass="android.widget.ShareActionProvider" />

【问题讨论】:

    标签: android


    【解决方案1】:

    像这样改变你的代码

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.menu_item_share:
    
    
        String playStoreLink = "https://play.google.com/store/apps/details?id=" +
        getPackageName();
        String yourShareText = "Install this app " + playStoreLink;
        Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setType("text/plain").setText(yourShareText).getIntent();
        // Set the share Intent
        mShareActionProvider.setShareIntent(shareIntent);
        startActivity(Intent.createChooser(shareIntent(), "Share via"));
            break;
    
        default:
            break;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    • onCreateOptionsMenu 将在您的活动调用时调用。
    • 您需要在按下Share button时启动分享意图`
    • 所以在onOptionsItemSelected中调用share Intent

    【讨论】:

    • 感谢 Mohsin,我在这行“startActivity(Intent.createChooser(shareIntent(), "Share via"));" 上遇到错误错误:shareIntent 方法未定义。
    • 最后删除()。像这样startActivity(Intent.createChooser(shareIntent, "Share via"));
    • 现在的问题是当我删除这个()时,菜单上的分享按钮不再可点击()
    【解决方案2】:

    您必须将这行代码startActivity(Intent.createChooser(shareIntent(), "Share via")); 移出到按钮单击处理程序或菜单选择处理程序中。

    将调用函数onCreateOptionsMenu() 来设置菜单,这将在您的Activity 启动时发生。

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多