0

我正在尝试在应用程序上实现共享图标以及我的溢出菜单。但是共享图标是灰色的,我无法单击它。

我的菜单 XML:

<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_action_share"
        android:actionProviderClass="android.widget.ShareActionProvider" />
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="About"/>
    <item
        android:id="@+id/action_help"
        android:orderInCategory="200"
        android:showAsAction="never"
        android:title="Help"/>
    <item
        android:id="@+id/action_rate"
        android:orderInCategory="300"
        android:showAsAction="never"
        android:title="Rate"/>
</menu>

我的菜单 Java 代码:

@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.menu_item_share);

        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
        String strMsg = "Blood Pressure is: \t\t" + tvSVal.getText().toString() + "/" + tvDVal.getText().toString();

        Intent myIntent5 = new Intent();
        myIntent5.setAction(Intent.ACTION_SEND);
        myIntent5.putExtra(Intent.EXTRA_TEXT, strMsg);
        myIntent5.setType("text/plain");

        mShareActionProvider.setShareIntent(myIntent5);

        return true;
    }

    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_help:
            //display the help activity
            Intent myIntent2 = new Intent(getApplicationContext(), HelpFile.class);
            startActivityForResult(myIntent2, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_about:
            //display the about window
            Log.i("Opening About Activity", "ABOUT");
            Intent myIntent = new Intent(MainActivity.this, AboutApp.class);
            startActivityForResult(myIntent, 0);
            overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
            return true;
        case R.id.action_rate:
            //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_VIEW);
            //Try Google play
            intent.setData(Uri.parse("market://details?id=com.test.testing"));
            if (MyStartActivity(intent) == false) {
                //Market (Google play) app seems not installed, let's try to open a web browser
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?com.test.testing"));
                if (MyStartActivity(intent) == false) {
                    //Well if this also fails, we have run out of options, inform the user.
                    //let the user know nothing was successful
                }
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    private boolean MyStartActivity(Intent aIntent) {
        try
        {
            startActivity(aIntent);
            return true;
        }
        catch (ActivityNotFoundException e)
        {
            return false;
        }
    }

我收到以下警告:The method setShareIntent(Intent) from the type MainActivity is never used locally

请帮我解决它。

4

1 回答 1

1

您需要设置ShareActionProvider. 创建菜单时,ShareActionProvider从菜单中保存一个实例。无论是在那个时候还是以后,通过setShareIntent(...). 有关更详细的说明和示例,请参阅此处的 Google 文档:http: //developer.android.com/training/sharing/shareaction.html

于 2014-01-02T17:32:27.737 回答