如何在android中实现共享 *** 作?

如何在android中实现共享 *** 作?,第1张

概述我想在我的项目中实现共享 *** 作,但是当我使用MenuItem时,它给出运行时错误而不是使用MenuItemCompat,但是它也给出了错误. 这是我的代码: @Overridepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it 我想在我的项目中实现共享 *** 作,但是当我使用MenuItem时,它给出运行时错误而不是使用MenuItemCompat,但是它也给出了错误.

这是我的代码:

@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.menu_third_,menu);    MenuItem menuItem = menu.findItem(R.ID.menu_item_share);    mShareActionProvIDer = (ShareActionProvIDer) menuItem.getActionProvIDer();    mShareActionProvIDer.setShareIntent(getDefaultShareIntent());    return true;}public Intent getDefaultShareIntent (){   Intent shareIntent = new Intent(Intent.ACTION_SEND);    shareIntent.setType("text/plain");    shareIntent.putExtra(Intent.EXTRA_SUBJECT,"SUBJECT");    shareIntent.putExtra(Intent.EXTRA_TEXT,"Extra Text");    return shareIntent;}
解决方法 我相信你已经为你的活动分类了AppCompatActivity,因为你得到了这个错误,请使用下面的代码

在您的活动中初始化ShareActionProvIDer

import androID.support.v7.Widget.ShareActionProvIDer;import androID.support.v4.vIEw.MenuItemCompat;    private ShareActionProvIDer mShareActionProvIDer;   @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main,menu);        // Locate MenuItem with ShareActionProvIDer        MenuItem item = menu.findItem(R.ID.action_share);        // Fetch and store ShareActionProvIDer        mShareActionProvIDer = (ShareActionProvIDer) MenuItemCompat.getActionProvIDer(item);        mShareActionProvIDer.setShareHistoryfilename(ShareActionProvIDer.DEFAulT_SHARE_HISTORY_file_name);        // Set share Intent.        // Note: You can set the share Intent afterwords if you don't want to set it right Now.        mShareActionProvIDer.setShareIntent(createShareIntent());        // Return true to display menu        return true;    }    // Create and return the Share Intent    private Intent createShareIntent() {        Intent shareIntent = new Intent(Intent.ACTION_SEND);        shareIntent.setType("text/plain");        shareIntent.putExtra(Intent.EXTRA_TEXT,"http://play.Google.com/store/apps/details?ID=pk.nimgade.Bostan.Train.Schedule");        Intent intent = Intent.createChooser(shareIntent,"Share");        return shareIntent;    }

这就是你的xml视图的样子

<item        androID:ID="@+ID/action_share"        app:actionProvIDerClass="androID.support.v7.Widget.ShareActionProvIDer"        app:showAsAction="always"        androID:title="Share" />

请确保你的SDK更新,在这个特殊的情况下,我确实碰到了,当你不知道缺少什么时,这是令人讨厌的

总结

以上是内存溢出为你收集整理的如何在android中实现共享 *** 作?全部内容,希望文章能够帮你解决如何在android中实现共享 *** 作?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1135336.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存