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

时间:2013-12-12 03:11:49

标签: java android xml

我正在尝试向我的应用添加共享菜单,将文本从textview 2发送到另一个应用程序I.E 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);
    }
} 

getMenuInflater().inflate(android.R.menu.main, menu);是问题

我的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>

1 个答案:

答案 0 :(得分:2)

<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>

应该放在菜单文件夹中的单独xml文件中(比如main.xml)。

而且,

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

应更正为

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