android想要在actionbar中添加searchview但app崩溃了

时间:2015-07-06 13:55:40

标签: android android-actionbar searchview

我在行动吧工作。我想在操作栏上添加searchview选项,但app崩溃了。

这是崩溃 enter image description here

这是我的选项菜单xml

的代码
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/search"
    android:title="Search"
    android:icon="@drawable/search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView"/>

这是searchable.xml的代码

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/enter"
android:includeInGlobalSearch="false"
android:label="@string/search"
android:searchSettingsDescription="@string/search_global_description" />

这是我的活动代码

    public class Thrd extends ActionBarActivity {
    Menu m;
    final Context context=this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thrd);

        getSupportActionBar().setTitle("3rd page");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#20a780"));
        getSupportActionBar().setBackgroundDrawable(colorDrawable);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate( R.menu.options_menu, menu );

        // Add SearchWidget.
        SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
        SearchView searchView = (SearchView) menu.findItem( R.id.search ).getActionView();

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

        return super.onCreateOptionsMenu( menu );
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                onSearchRequested();

                return true;
            case R.id.action_Exit:
                openExit();
                return true;
            default:
                return super.onOptionsItemSelected(item);
    }
}

    private void openExit() {

    }


}

请帮我解决我的问题。谢谢提前

2 个答案:

答案 0 :(得分:1)

这就是我实现搜索处理的方式。 在布局下的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="Search Trends" />

然后在您的ANDROID清单文件中,添加

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.tobisoft.trendify.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
//This is the meta to add for your activity
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>

此外,您还需要为搜索结果制作活动,如下所示:

<activity
android:name=".SearchResultsActivity"
android:label="@string/app_name"
android:theme="@style/MyMaterialTheme">
<!-- to identify this activity as "searchable" -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>

现在位于菜单文件夹中,文件名为main.xml(或您正在使用的任何内容)

<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="100"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />

您可以通过简单的Google搜索自行获取图标。您还需要将AppCompat库添加到项目中,您也可以通过简单的谷歌搜索来完成。在SearchResultsActivity中,添加以下内容:

public class SearchResultsActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
handleIntent(getIntent());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager =  
(SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search
}
}
}

activity_result.xml布局文件是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff141414">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="@dimen/_25sdp"
android:textStyle="bold"
android:text="Search Result goes here!"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

在您的MainActivity.java中添加以下内容:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView)menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}

所有这些帮助您的应用程序在正确的轨道上。希望它有所帮助

答案 1 :(得分:1)

image更改为导入android.support.v7.widget.SearchView帮助我并更改这些行

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView= (SearchView) searchItem.getActionView();
        searchView.setOnQueryTextListener(this);`