本文介绍了ActionBarSherlock - 搜索查看为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SherlockListActivity加入搜索功能,操作栏但是,当我尝试实例化搜索查看它总是空:

I am adding Search functionality to Action Bar in a SherlockListActivity however, when I try to instantiate SearchView its always null:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.activity_main_menu_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            public boolean onQueryTextSubmit(String p1){
                List<Application> auxList = Search.search(mContext, p1);
                AdapterApp aux = new AdapterApp(mContext, auxList);
                setListAdapter(aux);
                getListView().setOnItemClickListener(aux.getItemClickListener());
                return true;
            }

            public boolean onQueryTextChange(String p1){
                return false;
            }           
    });
    return super.onCreateOptionsMenu(menu);
}

activity_main_menu.xml:

activity_main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/activity_main_menu_search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView " />

    <item android:id="@+id/activity_main_menu_purchase"
        android:title="@string/purchase"
        android:icon="@drawable/ic_launcher_market_holo"
        android:showAsAction="ifRoom|collapseActionView" />

</menu>

此外,我不得不说,我已经正确导入所需的类:

Also I have to say that I have correctly imported required classes:

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnActionExpandListener;
import com.actionbarsherlock.widget.SearchView;

这是堆栈跟踪。当搜索查看被实例化,数收益这样的:

And this is stack trace. When SearchView is instantiated, log returns this:

06-03 19:42:43.128: W/dalvikvm(15936): dvmFindClassByName rejecting 'com.actionbarsherlock.widget.SearchView '
06-03 19:42:43.138: W/MenuInflater(15936): Cannot instantiate class: com.actionbarsherlock.widget.SearchView 
06-03 19:42:43.138: W/MenuInflater(15936): java.lang.ClassNotFoundException: Didn't find class "com.actionbarsherlock.widget.SearchView " on path: /data/app/com.iamaner.oneclickfreeze-1.apk
06-03 19:42:43.138: W/MenuInflater(15936):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.newInstance(MenuInflater.java:486)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:447)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.iamaner.oneclickfreeze.ActivityMain.onCreateOptionsMenu(ActivityMain.java:131)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.app.SherlockListActivity.onCreatePanelMenu(SherlockListActivity.java:184)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.app.SherlockListActivity.onCreateOptionsMenu(SherlockListActivity.java:149)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:458)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:820)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:248)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer.doFrame(Choreographer.java:531)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Handler.handleCallback(Handler.java:725)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Looper.loop(Looper.java:137)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.app.ActivityThread.main(ActivityThread.java:5231)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.reflect.Method.invoke(Method.java:511)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)

然后,搜索查看为空,因此调用SearchView.setOnQueryTextListener()时,我得到一个NullPointerException异常;

Then, SearchView is null, so I get a NullPointerException when calling SearchView.setOnQueryTextListener();

06-03 19:42:43.148: E/AndroidRuntime(15936): FATAL EXCEPTION: main
06-03 19:42:43.148: E/AndroidRuntime(15936): java.lang.NullPointerException
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.iamaner.oneclickfreeze.ActivityMain.onCreateOptionsMenu(ActivityMain.java:133)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.app.SherlockListActivity.onCreatePanelMenu(SherlockListActivity.java:184)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.app.SherlockListActivity.onCreateOptionsMenu(SherlockListActivity.java:149)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:458)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:820)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:248)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer.doFrame(Choreographer.java:531)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Handler.handleCallback(Handler.java:725)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Looper.loop(Looper.java:137)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.app.ActivityThread.main(ActivityThread.java:5231)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at java.lang.reflect.Method.invoke(Method.java:511)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)

我已经浪费了,每天搜索错误的原因,但没有成功...请我需要帮助

I have wasted all a day searching the cause of the error, but no success... Please I need help

推荐答案

好了,本身是由ActionBarSherlock库导致了错误,你可以在GitHub上这里的问题:https://github.com/JakeWharton/ActionBarSherlock/issues/685

Well, the error itself is caused by ActionBarSherlock library, you can find the issue in GitHub here: https://github.com/JakeWharton/ActionBarSherlock/issues/685

和张贴在这里评论是我的解决方案:
https://github.com/JakeWharton/ActionBarSherlock/issues/685#issuecomment-11670533

And the comment posted here was my solution: https://github.com/JakeWharton/ActionBarSherlock/issues/685#issuecomment-11670533

转到RES /值-V14 / ABS库项目的abs_themes.xml:那么你应该先修改两个主题是这样的:

Go to res/values-v14/abs_themes.xml of ABS library project: then you should edit first two themes like this:

<style name="Sherlock.__Theme" parent="android:Theme.Holo">
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
</style>
<style name="Sherlock.__Theme.Light" parent="android:Theme.Holo.Light">
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
</style>

另外不要忘记在你的清单声明对于那些使用它的活动:

Also dont forget to declare in your manifest for those activities which use it:

android:theme="@style/Theme.Sherlock.Light"

这篇关于ActionBarSherlock - 搜索查看为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 09:13