1

i have been trying to figure out how to make a simple search button. I found this (Implementing SearchView in action bar) which i thought would help and i guess it did, i just cant figure out why there is an error here:

SearchableActivity.java

package com.ryan.buttonsimple;

import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;


public class SearchableActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) findItemById(R.id.search_view); //**HERE IS THE ERROR! "findItemById" and "search_view" "Cannot resolve method for both**
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_searchable, menu);
        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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

here is my searchable.xml file:

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


</searchable>

And here is my AndroidManifest.xml File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ryan.buttonsimple" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SearchableActivity"
            android:label="@string/title_activity_searchable" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

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

</manifest>
Community
  • 1
  • 1
Stillie
  • 2,647
  • 6
  • 28
  • 50
  • if you look in the SearchableActivity.java section the line SearchView searchView = (SearchView) findItemById(R.id.search_view); It says cannot resolve method? – Stillie Feb 22 '15 at 12:39
  • Well you don't declare it, and neither does ActionBarActivity, so why should it resolve? – Adrian Leonhard Feb 22 '15 at 12:41
  • the SearchView is usally declared as part of `menu/name.xml` , inflated in `onCreateOptionsMenu`. What do you think will happen if onCreate is called before `onCreateOptionsMenu`? – Blackbelt Feb 22 '15 at 12:42
  • You probably want findViewById... doesn't your IDE suggest this to you? – Adrian Leonhard Feb 22 '15 at 12:42
  • Please post the stack trace from logcat. This tells you exactly what the error is. Without, we're just guessing. – Simon Feb 22 '15 at 13:08
  • im using Android Studio, ive got the errors in Gradle as below: error: cannot find symbol variable search_view error: cannot find symbol variable menu_searchable Execution failed for task 'app.compileDebugJava'. >Compilation failed; see the compiler error output for details. – Stillie Feb 22 '15 at 16:55
  • call `onSearchRequested()` inside onCreate() method – rogerwar Feb 24 '15 at 11:01

0 Answers0