Android支持库ActionBar无法在2.3设备中使用

时间:2013-07-26 05:32:20

标签: android android-actionbar compatibility android-support-library searchview

朋友您好我用v7 Appcompact实现了ActionBar支持库。我用ActionBarActivity扩展我的Activity类。以下是我的主要课程

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    MenuItem menuItem=menu.findItem(R.id.menu_settings);
    SearchView mSearchView=(SearchView)menuItem.getActionView();
    return super.onCreateOptionsMenu(menu);
       }

    }

在Manifest文件中,我声明了像这样的主题类型

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appcompactdemo"
android:versionCode="1"
android:versionName="1.0" >

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

在style.xml中,如下所示:

<style name="ExampleTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="homeAsUpIndicator">@drawable/new_indicator</item>
    <item name="android:homeAsUpIndicator">@drawable/new_indicator</item>
  </style>

以上代码在Android版本4.0中运行良好,但在2.3设备中它无法正常工作,它会出现如下错误:

java.lang.NoSuchMethodError: android.view.MenuItem.getActionView
    07-26 04:11:40.900: E/AndroidRuntime(412):  at com.example.appcompactdemo.MainActivity.onCreateOptionsMenu(MainActivity.java:24)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224) 
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:69)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.os.Handler.handleCallback(Handler.java:587)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.os.Looper.loop(Looper.java:130)
07-26 04:11:40.900: E/AndroidRuntime(412):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-26 04:11:40.900: E/AndroidRuntime(412):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 04:11:40.900: E/AndroidRuntime(412):  at java.lang.reflect.Method.invoke(Method.java:507)
07-26 04:11:40.900: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-26 04:11:40.900: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-26 04:11:40.900: E/AndroidRuntime(412):  at dalvik.system.NativeStart.main(Native Method)

所以我知道如何解决它?

1 个答案:

答案 0 :(得分:48)

我认为问题的答案在official developers guide

您应该声明搜索小部件

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

如果需要配置操作视图(例如添加事件侦听器),则可以在onCreateOptionsMenu()回调期间执行此操作。您可以通过调用静态方法MenuItemCompat.getActionView()并将相应的MenuItem传递给它来获取操作视图对象。例如,上面的示例中的搜索小部件是这样获得的:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); // <-- change your code to this
    // Configure the search info and add any event listeners
    ...
    return super.onCreateOptionsMenu(menu);
}