Philip435 0 Newbie Poster

Hey,

Im trying to implement a search in the action bar in my android app. Ive got the serachView to show and Im able to write in it. But when I press enter to make it submit the request should just open my SearchActivity were I should just be able to handle the request. The problem is that the searchActivity gets called 2 times where the second time it makes the app crash.

hHas anyone had the same problem ?

The Manifest:

        <activity
            android:name="com.example.qrepublic.MenuActivity"
            android:label="@string/menu_title"
            android:theme="@android:style/Theme.Holo.Light" >
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchActivity" />
        </activity>

        <activity
            android:name="com.example.qrepublic.SearchActivity"
            android:label="@string/search_title"
            android:parentActivityName="com.example.qrepublic.MenuActivity"
            android:theme="@android:style/Theme.Holo.Light" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

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

The MenuActivity:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        return true;
    }

The SearchActivity:

package com.example.qrepublic;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class SearchActivity extends ListActivity{

    private static final String TAG = "SEARCH";
    private static final int requestID = 99;
    private Context context;

    ServerRequest request;
    ListArrayAdapter bookedarray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }



    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          new Connect().execute(query);
        }
    }


    private class Connect extends AsyncTask<String, Integer, ArrayList<String[]>>{

        @Override
        protected ArrayList<String[]> doInBackground(String... arg) {
            try {

                JSONArray array = new JSONArray();
                JSONObject jo = new JSONObject();
                JSONObject to = new JSONObject();

                try {
                    jo.put("request", requestID);
                    to.put("quary", arg[0]);
                    jo.put("data", to);
                } catch (JSONException e) {
                    Log.e(TAG, "failed to create a JSON to send to the server", e);
                }
                array.put(jo);

                request = new com.example.qrepublic.ServerRequest(array);

                JSONArray Jarray = request.getServerData();
                if (Jarray == null){return null;}
                JSONHandler handler = new JSONHandler(Jarray);              
                return handler.toArrayList();


            } catch (Exception e) {
                Log.e(TAG, "exception ", e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<String[]> result) {
            if(result != null){
                bookedarray = new ListArrayAdapter(context, result);
                setListAdapter(bookedarray);
            }
        }   
    }

}

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search_label"
    android:hint="@string/search_hint" 
    android:searchMode="showSearchLabelAsBadge"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:voiceLanguageModel="free_form"
    android:voicePromptText="@string/search_invoke"
    android:searchSuggestSelection=" ? "
/>

menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_search"
          android:title="@string/menu_search"
          android:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.widget.SearchView" />
</menu>