操作栏搜索视图(Android)null错误

时间:2014-03-03 01:11:27

标签: android android-fragments android-actionbar

我不确定为什么它出现为null,如果它必须对片段和片段标题或抽屉做某事,因为它是在这个片段被拉出的主要活动上。

这是我的菜单xml项目:

<?xml version="1.0" encoding="utf-8"?>
<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:title="@string/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

这是片段代码:

public class BrowseFragment extends Fragment {

    //action bar expands to search
    //search - keyword or tab search, title
    //include popular - same algorithm as home
    //show categories

    //category click takes you to grid activity
//adapter with custom model
    private CategoryViewerAdapter adapter;

    //list with custom model
    private List<CategoryItem> categoriesList;

    //List view
    private ListView categoryListView;

    //SearchView
    private SearchView search;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.browse, menu);

        //find the menu item and set search view at the same time
        search = (SearchView) menu.findItem(R.id.action_search).getActionView();


        //to always show the search view - to include this must implement it above
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
        search.setIconified(false);

        super.onCreateOptionsMenu(menu,inflater);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_browse, container, false);

        //Allows menu options in fragment
        setHasOptionsMenu(true);


        //Set adapter for multi-select model and adapter
        adapter = new CategoryViewerAdapter(getActivity(), getSingleCategory());
        categoryListView = (ListView) rootView.findViewById(R.id.categoryListView);
        categoryListView.setAdapter(adapter);


        return rootView;
    }

    //call to retrieve list
    private List<CategoryItem> getSingleCategory() {
        //initiate new list
        categoriesList = new ArrayList<CategoryItem>();

        //called to get string array
        Resources res = getResources();
        String[] categories = res.getStringArray(R.array.categories);

        //loop through string array
        for (String category : categories) {
            categoriesList.add(get(category));
        }

        return categoriesList;
    }

    //call model to set names and methods for each item
    private CategoryItem get(String name) {
        return new CategoryItem(name);
    }


}

空指针错误指向此行:

search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

以下是错误日志:

03-03 02:50:33.590  15744-15744/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.app, PID: 15744
    java.lang.NullPointerException
            at com.example.app.BrowseFragment.onCreateOptionsMenu(BrowseFragment.java:64)
            at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1780)
            at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1927)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2539)
            at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
            at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
            at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:543)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:30)

您使用的是Proguard吗?如果是这样,请确保您有一个保留android.support.v7.widget.SearchView类的规则。

例如,要将所有不同版本的支持库中的所有内容保留,请添加:

-keep class android.support.** { *; }
-keep interface android.support.** { *; }

到您的proguard-rules.txt文件。

答案 1 :(得分:0)

我遇到了同样的问题。我的解决方案在菜单中。

我有:

app:actionViewClass="android.support.v4.widget.SearchView"

我什么时候应该

app:actionViewClass="android.support.v7.widget.SearchView"

答案 2 :(得分:0)

我的错误是MenuInflater的初始化错误。

MenuInflater inflater = getMenuInflater();

更正问题解决后。

MenuInflater inflater = new MenuInflater(getApplicationContext());