Android MenuItemCompat.getActionView(MenuItem)返回null

时间:2014-12-17 12:28:57

标签: android

我正在尝试集成v7 appcompat库来实现ActionBar。我能够充气   带有菜单项的操作栏但问题是我无法获得对菜单项的引用   使用MenuItemCompat.getActionView()调用。它返回null。

这是我的代码。

@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 copyItem = menu.findItem(R.id.action_copy);        
    TextView copyView = (TextView) MenuItemCompat.getActionView(copyItem);



    return true;
}

main.xml中   .........................

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_search"
    android:title="Search"/>
<item
    android:id="@+id/action_copy"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_content_copy"
    android:title="Copy"/>
<item
    android:id="@+id/action_share"
    android:orderInCategory="100"
    app:showAsAction="always"
    android:icon="@drawable/ic_social_share"
    android:title="Share"/>

styles.xml

.................

 <resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

 manifest.xml

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.hmkcode.android.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

请告诉我遗失的地方。

由于

1 个答案:

答案 0 :(得分:1)

  

请告诉我遗失的地方。

您没有在菜单XML资源中定义的操作视图。因此,getActionView()应该返回null

如果您希望getActionView()返回非null值,则需要关注the documentation并向菜单XML资源添加操作视图,例如:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_action_search"
          yourapp:showAsAction="ifRoom|collapseActionView"
          yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>