Search Query does not change photos


#1

I have been on this all day going line by line from the example to my code. I give up. I press the magnifying glass at the top of the app. The screen changes to accept an entry to search. I enter Cat for example and press the enter key. Nothing ever changes. When I debug I never see PhotoGalleryActivity.onNewIntent called which I think is where the search value is supposed to be stored to be passed in the variable “query”. I don’t see any code that is listening for the Enter Key being pressed. The only time I see different pictures is if I hard code the word “Cats” in the variable “query”. How do I even start to debug this? I can’t even imagine what I have done wrong. Why doesn’t onNewIntent seem to be used? Is the Enter key the right key to press to activate the search? Where do I start? I know you are going to ask for sample code, but what java file am I even going to cut from?


#2

Not sure if this is what’s causing your problem, but I experienced something similar. Have a look in logcat and see if you’re getting an XMLPullParser exception when performing a search. I was, but it didn’t cause the app to exit, just didn’t load any of the photos from the search. I had to add a hack to strip off invalid chars at the beginning of the XML feed.


#3

I happen to meet this problem too. Finally I find my problem is when I create searchable.xml, I use the default template provided by Android SDK. Then my searchable.xml is like this.

<?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>
In eclipse, the two rows of properties are in plain text, which I guess they don’t work. So I change the searchable.xml to be the same as stated on book.

<?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" />
Then onNewIntent method works and I can search in the app. Hope my experience could help you.


#4

For me, I mistyped some of the code checking for the Intent action in PhotoGalleryActivity which caused the same faulty behavior.

    @Override
    protected void onNewIntent(Intent intent) {
        PhotoGalleryFragment fragment = (PhotoGalleryFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_container);

        if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.i(TAG, "Received a new search query: " + query);

            // Other SharedPreferences code later in the chapter
        }

        fragment.updateItems();
    }

Instead of intent.getAction().equals(Intent.ACTION_SEARCH), I had getIntent().getAction(). As explained in the chapter, getIntent() will always return the Intent which started the Activity instead of the most recent Intent received by the Activity.