简体   繁体   English

android要在动作栏中添加searchview但应用程序崩溃

[英]android want to add searchview in actionbar but app crash

i am working on action bar . 我在动作栏上工作。 i want to add searchview option on the action bar but app crash here. 我想在操作栏上添加searchview选项,但此处应用崩溃。

this is crash 这是崩溃 在此处输入图片说明

this is my code of option menu xml 这是我的选项菜单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"/>

this is the code of searchable.xml 这是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" />

and this is my activity code 这是我的活动代码

    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() {

    }


}

please help me to solve my problem.thanks advance 请帮助我解决我的问题。谢谢前进

This was how I implemented my search handling. 这就是我实施搜索处理的方式。 In the XML folder under layout, add a searchable.xml file and put this code like so: 在布局下的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" />

Then in your ANDROID manifest file, add 然后在您的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>

Also you would need to make an activity for the search results like so: 另外,您还需要为搜索结果进行活动,如下所示:

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

now in the menu folder, with a file name main.xml (or anything you are using) 现在位于菜单文件夹中,文件名为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" />

You can get the icon yourself by a simple google search. 您可以通过简单的Google搜索自己获取图标。 You would also need to add the AppCompat Library to your project, something you can also do from a simple google search. 您还需要将AppCompat库添加到您的项目中,也可以通过简单的Google搜索来完成。 In the SearchResultsActivity, add this: 在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
}
}
}

The activity_result.xml layout file is this : 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>

In your MainActivity.java add this: 在您的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;
}

All this shld help your app on the right track. 所有这些都可以帮助您的应用程序步入正轨。 Hope it helps 希望能帮助到你

图片 Changing to import android.support.v7.widget.SearchView helped me and also change these lines 更改为导入android.support.v7.widget.SearchView不仅对我有所帮助,而且还更改了这些行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM