Android的 - NullPointerException异常对搜索查看的操作栏 [英] Android - NullPointerException on SearchView in Action Bar

查看:231
本文介绍了Android的 - NullPointerException异常对搜索查看的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,尝试将搜索查看小部件添加到动作条在我的活动 - 调用getActionView的时候,让我的搜索查看对象,我得到一个空值

我一直在关注Android的开发者指南,也经历了大量的做题,以及互联网上的其他环节;都没有用。这可能是一些简单,但我无法弄清楚 - 在我看来,我的code是基本相同的,从谷歌(除了改变一些名称等),但它仍然是行不通的。

任何帮助将是AP preciated。

下面是包括的code相关位。请让我知道如果有什么不清楚,如果你有任何想法,什么可能是错误的。

活动code:

 私人的ListView workflowListView;
    私人DrawerLayout drawerLayout;
    私人的ListView drawerList;
    私人ActionBarDrawerToggle drawerToggle;

    @覆盖
    保护无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);

        。getWindow()requestFeature(Window.FEATURE_ACTION_BAR);
        的setContentView(R.layout.activity_workflow_list);

        workflowListView =(ListView控件)findViewById(R.id.workflowListView);
        drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        drawerList =(ListView控件)findViewById(R.id.drawer_list);

        drawerToggle =新ActionBarDrawerToggle(
             此时,/ *主机活动* /
             drawerLayout,/ * DrawerLayout对象* /
             R.drawable.ic_launcher,/ *导航抽屉图标来取代上插入符* /
             R.string.app_name,/ *开放抽屉的描述* /
             R.string.app_name / *关闭抽屉里的描述* /
             ){

                / **调用时抽屉处于完全封闭的状态下稳定运行。 * /
                公共无效onDrawerClosed(查看视图){
    // getActionBar()的setTitle(封闭的抽屉)。
                }

                / **调用时的抽屉里一个完全开放的状态下稳定运行。 * /
                公共无效onDrawerOpened(查看drawerView){
    // getActionBar()的setTitle(打开抽屉)。
                }
       };

       drawerLayout.setDrawerListener(drawerToggle);
       动作条动作条= getActionBar();
       actionBar.setDisplayShowTitleEnabled(假);

       actionBar.setDisplayHomeAsUpEnabled(真正的);
       actionBar.setHomeButtonEnabled(真正的);
       actionBar.setIcon(android.R.color.transparent);

       串[] TESTDATA = {一个,B,C,D};
       ArrayList的<字符串> workflowList =新的ArrayList<字符串>();
           对于(字符串S:TESTDATA){

                workflowList.add(多个);
           }

       ArrayAdapter<字符串> workflowAdapter =新的ArrayAdapter<字符串>(this.getApplicationContext(),R.layout.workflow_list_item,workflowList);

            workflowListView.setAdapter(workflowAdapter);
   }


   @覆盖
   公共布尔onCreateOptionsMenu(功能菜单){
   //充气菜单;这增加了项目操作栏,如果它是present。
        MenuInflater充气= getMenuInflater();

        inflater.inflate(R.menu.workflow_list,菜单);
        inflater.inflate(R.menu.options_menu,菜单);

        //关联搜索的配置与搜索查看
        SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);

        //下面一行返回null即使它是在谷歌样品code使用
        搜索查看搜索查看=(搜索查看)menu.findItem(R.id.search).getActionView();

        sea​​rchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        返回super.onCreateOptionsMenu(菜单);
    }
 

XML / searchable.xml:

 < XML版本=1.0编码=UTF-8&GT?;

<可搜索的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:标签=@字符串/ APP_NAME
        机器人:提示=@字符串/ search_hint
        机器人:includeInGlobalSearch =FALSE/>
 

菜单/ options_menu.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 /搜索
          机器人:标题=@字符串/ search_title
          机器人:图标=@可绘制/ ic_action_search
          机器人:showAsAction =永远| collapseActionView
          机器人:actionViewClass =android.support.v7.widget.SearchView/>
< /菜单>
 

解决方案

试着用替换故障线路:

  mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView =(EnglishVerbSearchView)MenuItemCompat.getActionView(mSearchMenuItem);
 

在哪里R.id.action_search是你的搜索项在菜单中的ID。

修改

您的清单应该看起来像:

 <活动
       机器人:名称=com.bronzelabs.twc.activities.WorkflowListActivity
       机器人:标签=@字符串/ APP_NAME
       机器人:主题=@风格/ Theme.AppCompat.Light>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
< /活性GT;
<活动
        机器人:名称=。activities.SearchResultActivity
        机器人:主题=@风格/ Theme.AppCompat.Light
        机器人:launchMode =singleTop>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.SEARCH/>
        &所述; /意图滤光器>
        <元数据的android:NAME =android.app.searchable安卓资源=@ XML /搜索
            activities.SearchResultActivity:机器人值= />
    < /活性GT;
 

您拨打 setSearchableInfo 的方法是:

  mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
            新单元名(getApplicationContext(),SearchResultActivity.class)));
 

编辑2

请确保您的菜单的XML文件是这样的(注意那些2属性与 yourapp <​​/ code>命名空间 - 当你使用compat的图书馆,这是需要的):

 &LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 的xmlns:yourapp =htt​​p://schemas.android.com/apk/res-auto&GT;
    &LT;项目机器人:ID =@ + ID / ACTION_SEARCH
      机器人:标题=@字符串/ ACTION_SEARCH
      机器人:图标=@可绘制/ ACTION_SEARCH
      yourapp:showAsAction =永远| collapseActionView
      yourapp:actionViewClass =android.support.v7.widget.SearchView/&GT;
&LT; /菜单&gt;
 

I've got a problem trying to add a SearchView widget to the ActionBar in my activity - I get a null value when calling getActionView to get my SearchView object.

I've been following the Android Developer guide and also went through a ton of SO questions as well as some other links on the Internet; all to no avail. It could be something simple but I wasn't able to figure it out - it seems to me my code is basically the same as that from google (apart from changing some names etc.) but it still won't work.

Any help would be appreciated.

Included below are relevant bits of the code. Please let me know if anything's unclear of if you have any idea as to what could possibly be wrong.

Activity code:

    private ListView workflowListView;
    private DrawerLayout drawerLayout;
    private ListView drawerList;
    private ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_workflow_list);

        workflowListView = (ListView) findViewById(R.id.workflowListView);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.drawer_list);

        drawerToggle = new ActionBarDrawerToggle(
             this,                  /* host Activity */
             drawerLayout,         /* DrawerLayout object */
             R.drawable.ic_launcher,  /* nav drawer icon to replace 'Up' caret */
             R.string.app_name,  /* "open drawer" description */
             R.string.app_name  /* "close drawer" description */
             ) {

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
    //                getActionBar().setTitle("Closed drawer");
                }

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
    //                getActionBar().setTitle("Open drawer");
                }
       };

       drawerLayout.setDrawerListener(drawerToggle);
       ActionBar actionBar = getActionBar();
       actionBar.setDisplayShowTitleEnabled(false);

       actionBar.setDisplayHomeAsUpEnabled(true);
       actionBar.setHomeButtonEnabled(true);
       actionBar.setIcon(android.R.color.transparent);

       String[] testData = {"a", "b", "c", "d"};
       ArrayList<String> workflowList = new ArrayList<String>();
           for (String s : testData) {

                workflowList.add(s);
           }

       ArrayAdapter<String> workflowAdapter = new ArrayAdapter<String>(this.getApplicationContext(), R.layout.workflow_list_item, workflowList);

            workflowListView.setAdapter(workflowAdapter);
   }


   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.workflow_list, menu);
        inflater.inflate(R.menu.options_menu, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        // The below line returned null even though it was used in Google sample code
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        return super.onCreateOptionsMenu(menu);
    }

xml/searchable.xml:

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint"
        android:includeInGlobalSearch="false" />

menu/options_menu.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/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_action_search"
          android:showAsAction="always|collapseActionView"
          android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

解决方案

Try to replace the failing line with:

mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (EnglishVerbSearchView) MenuItemCompat.getActionView(mSearchMenuItem);

Where R.id.action_search is the id of your search item in the menu.

EDIT

Your manifest should look like that:

<activity
       android:name="com.bronzelabs.twc.activities.WorkflowListActivity"
       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>
<activity 
        android:name=".activities.SearchResultActivity"
        android:theme="@style/Theme.AppCompat.Light"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"
            android:value=".activities.SearchResultActivity" />
    </activity>

The way you call setSearchableInfo is:

mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
            new ComponentName(getApplicationContext(), SearchResultActivity.class)));

EDIT 2

Make sure your menu xml file is like that (pay attention to those 2 attributes with yourapp namespace - this is needed when you use the compat library):

<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/action_search"
      yourapp:showAsAction="always|collapseActionView"
      yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

这篇关于Android的 - NullPointerException异常对搜索查看的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆