无法实现搜索小部件

时间:2016-01-18 10:36:01

标签: android search nullpointerexception appbar

我试图在我的应用中实现搜索小部件。我从here找到了一个有用的教程。

My Activity A

但我的应用程序崩溃了。

活动A

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.create_menu, menu);
            // Associate searchable configuration with the SearchView
            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
            return true;
        }

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {

            case R.id.add: // create new file
                View menuItemView = findViewById(R.id.add);
                PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        //  Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        if ("Create New File".equals(item.getTitle())) {
                            Intent intent = new Intent(HomePage.this, Information.class);  // go to Information class
                            startActivity(intent);

                        } else if ("Edit File".equals(item.getTitle())) {
                            Intent intent = new Intent(HomePage.this, Edit.class);
                            startActivity(intent);
                        }
                        return true;
                    }
                });
                po.show(); //showing popup menu

        }
        return super.onOptionsItemSelected(item);
    }

可在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="Search by date" />

create_menu

<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="Search by date"
        android:icon="@mipmap/search"
        app:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />

    <item
        android:icon="@mipmap/menu"
        android:id="@+id/add"
        android:orderInCategory="100"
        android:title="Main Menu"
        app:showAsAction="always" />

</menu>

在mainfest

中声明
  <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

错误LogCat

01-18 18:31:09.298    9215-9215/com.example.project.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.myapplication, PID: 9215
    java.lang.NullPointerException
            at com.example.project.myapplication.GUI.HomePage.onCreateOptionsMenu(HomePage.java:104)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2646)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallback

这是错误指向searchView.setSearchableInfo(

的行

1 个答案:

答案 0 :(得分:1)

您应该使用支持SearchView的支持版本:

<item android:id="@+id/action_search"
    android:title="@string/search_title"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />

并且您在Manifest.xml中添加了正确的Intent Filter

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />