简体   繁体   English

Android:分享菜单,我的菜单inflater无法解析我的资源

[英]Android: Share Menu, My menu inflater cannot Resolve my resource

I am trying to add a share menu to my app that sends text from textview 2 to another app IE KiK but currently i cannot get it to identify the Resource file as the menu heres my code: 我正在尝试向我的应用程序添加共享菜单,将文本从textview 2发送到另一个应用程序IE KiK,但目前我无法识别资源文件作为菜单继承我的代码:

public class ME extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

       getMenuInflater().inflate(android.R.menu.main, menu);
       MenuItem shareItem = menu.findItem(R.id.action_share);

        ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Hi");
        mShare.setShareIntent(shareIntent);

        return super.onCreateOptionsMenu(menu);
    }
} 

The getMenuInflater().inflate(android.R.menu.main, menu); getMenuInflater().inflate(android.R.menu.main, menu); is the problem 是问题

And my XML Resource file 和我的XML资源文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >

     <TextView
        android:="@+id/TextView 1"
        android:orientation="vertical"
        android:layout_width="400dp"
        android:layout_height="300dp" >
     </TextView>

     <TextView
        android:="@+id/TextView 2"
        android:orientation="vertical"
        android:layout_width="400dp"
        android:layout_height="300dp" >
     </TextView>

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:android="http://schemas.android.com/apk/res-auto" >
        <item android:id="@+id/action_share"
            android:orderInCategory="100"
            android:icon="@drawable/ic_menu_share"
            android:actionProviderClass= "android.widget.ShareActionProvider" />
   </menu>
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:android="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_share"
          android:orderInCategory="100"
          android:icon="@drawable/ic_menu_share"
          android:actionProviderClass= "android.widget.ShareActionProvider"
           />
   </menu>

Should be put in a seperate xml file in the menu folder (say main.xml). 应该放在菜单文件夹中的单独的xml文件中(比如main.xml)。

And, 和,

getMenuInflater().inflate(android.R.menu.main, menu);

should be corrected as 应该更正为

getMenuInflater().inflate(R.menu.main, menu);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM