操作栏搜索查看(安卓)零误差误差、操作

2023-09-12 02:20:49 作者:Glu TtoNy |▏沉沦

我不知道为什么会被显示为空,如果有做一些事情的片段和片段标题或抽屉,因为它是在哪里这个片段被拉升的主要活动。

下面是我的菜单XML项:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>
    <项目机器人:ID =@ + ID / ACTION_SEARCH
        机器人:标题=@字符串/ ACTION_SEARCH
        机器人:图标=@可绘制/ ic_action_search
        应用程序:showAsAction =总是
        应用程序:actionViewClass =android.support.v7.widget.SearchView/>
< /菜单>
 

下面是片段code:

 公共类BrowseFragment扩展片段{

    //操作栏扩展到搜索
    //搜索 - 关键字或标签搜索,标题
    //包括流行的 - 相同的算法如家
    //显示分类

    //类别点击带您到网格活动
//适配器自定义模型
    私人CategoryViewerAdapter适配器;

    //列表自定义模型
    私人列表< CategoryItem> categoriesList;

    //列表显示
    私人的ListView categoryListView;

    //搜索查看
    私人搜索查看搜索;

    @覆盖
    公共无效onCreateOptionsMenu(功能菜单,MenuInflater充气){
        inflater.inflate(R.menu.browse,菜单);

        //发现在同一时间菜单项,并设置搜索视图
        搜索=(搜索查看)menu.findItem(R.id.action_search).getActionView();


        //始终显示在搜索视图 - 包括这上面必须实现它
        sea​​rch.setOnQueryTextListener(新SearchView.OnQueryTextListener(){
            @覆盖
            公共布尔onQueryTextSubmit(String s)将{
                返回false;
            }

            @覆盖
            公共布尔onQueryTextChange(String s)将{
                返回false;
            }
        });
        sea​​rch.setIconified(假);

        super.onCreateOptionsMenu(菜单,充气);
    }


    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                             捆绑savedInstanceState){
        查看rootView = inflater.inflate(R.layout.fragment_browse,集装箱,假);

        //允许在片段菜单选项
        setHasOptionsMenu(真正的);


        //设置适配器多选模式和适配器
        适配器=新CategoryViewerAdapter(getActivity(),getSingleCategory());
        categoryListView =(ListView控件)rootView.findViewById(R.id.categoryListView);
        categoryListView.setAdapter(适配器);


        返回rootView;
    }

    //调用来检索列表
    私人列表< CategoryItem> getSingleCategory(){
        //启动新的列表
        categoriesList =新的ArrayList< CategoryItem>();

        //调用以获取字符串数组
        资源RES = getResources();
        的String []类别= res.getStringArray(R.array.categories);

        //通过字符串数组循环
        对于(字符串类:类别){
            categoriesList.add(获取(类));
        }

        返回categoriesList;
    }

    //调用模型设置名称和方法,为每个项目
    私人CategoryItem获取(字符串名称){
        返回新CategoryItem(名称);
    }


}
 
Excel怎么使用误差线制作梯形图

空指针错误指向该行:

  search.setOnQueryTextListener(新SearchView.OnQueryTextListener(){
 

下面是错误日志:

  2月3日至3日:50:33.590 15744-15744 /? E / AndroidRuntime:致命异常:主要
    工艺:com.example.app,PID:15744
    显示java.lang.NullPointerException
            在com.example.app.BrowseFragment.onCreateOptionsMenu(BrowseFragment.java:64)
            在android.app.Fragment.performCreateOptionsMenu(Fragment.java:1780)
            在android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1927)
            在android.app.Activity.onCreatePanelMenu(Activity.java:2539)
            在com.android.internal.policy.impl.PhoneWindow。preparePanel(PhoneWindow.java:436)
            在com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
            在com.android.internal.policy.impl.PhoneWindow $ 1.运行(PhoneWindow.java:221)
            在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)
            在android.view.Choreographer.doCallbacks(Choreographer.java:574)
            在android.view.Choreographer.doFrame(Choreographer.java:543)
            在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)
            在android.os.Handler.handleCallback(Handler.java:733)
            在android.os.Handler.dispatchMessage(Handler.java:95)
            在android.os.Looper.loop(Looper.java:136)
            在android.app.ActivityThread.main(ActivityThread.java:5017)
            在java.lang.reflect.Method.invokeNative(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:515)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            在dalvik.system.NativeStart.main(本机方法)
 

解决方案

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

规则

例如,把一切都所有不同版本的支持库,添加:

  -keep类android.support ** {*。 }
-keep接口android.support ** {*。 }
 

你的 ProGuard的-rules.txt 文件。

I am not sure why it is appearing as null, if it has to do something with the fragment and the fragment title or the drawer, since it is on the main activity of where this fragment is being pulled up from.

Here is my menu xml item:

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

Here is the fragment code:

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);
    }


}

The null pointer error points to this line:

search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

Here is the error log:

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)

解决方案

Are you using Proguard? If so, make sure you have a rule that keeps the android.support.v7.widget.SearchView class.

For example, to keep everything in all the different versions of the support library, add:

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

to your proguard-rules.txt file.