Search code examples
autocompleteactionbarsherlockgoogle-maps-android-api-2searchview

Android SearchView widget only works for Launcher Activity


I'm facing a very weird error right now. Basically, the app can search Google Places API using SearchView widget and show the results in Google Maps Android API V2, and the searchview widget is placed in action bar. I have referenced to this tutorial: http://wptrafficanalyzer.in/blog/android-searchview-widget-with-google-places-api-using-actionbarsherlock-library/

The app works fine when the searchable activity (I name it as SearchPlace) is the main and launcher activity. However, if I try to intent to this activity from another activity, the app crash. I got NullPointerException caused by handleIntent(). I have attached a portion of the Manifest file below SearchPlace is the name of my searchable activity.

<!-- Protect the map component of the application using application signature -->
<permission
    android:name="in.wptrafficanalyzer.locationsherlocksearchviewmapv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<!-- Allows to receive map -->
<uses-permission android:name="in.wptrafficanalyzer.locationsherlocksearchviewmapv2.permission.MAPS_RECEIVE" />

<!-- Used by the Google Maps Android API V2 to download map tiles from Google Maps servers -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Allows the Google Maps Android API V2 to cache map tile data in the device's external storage area -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Allows the Google Maps Android API V2 to use WiFi or mobile cell data (or both) to determine the device's location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- Allows the Google Maps Android API V2 to use the Global Positioning System (GPS)
to determine the device's location to within a very small area -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Allows to contact Google Serves -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<!-- Google Maps Android API V2 requires OpenGL ES version 2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock" >
    <activity
        android:name="in.wptrafficanalyzer.locationsherlocksearchviewmapv2.Home"
        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="in.wptrafficanalyzer.locationsherlocksearchviewmapv2.SearchPlace"
        android:label="@string/app_name"
        android:launchMode="singleTop"
       >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <!-- Points to searchable activity -->
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchPlace" />

        <!-- Points to searchable meta data -->
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>
    <provider
        android:name=".PlaceProvider"
                    android:authorities="in.wptrafficanalyzer.locationsherlocksearchviewmapv2.PlaceProvider"
        android:exported="false" />

    <!-- Specifies the Android API Key, which is obtained from Google API Console -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="YOUR_ANDROID_API_KEY" />
</application>

The error log is attached below. It seems like my handleIntent() caused the error.

07-31 01:49:53.685: E/AndroidRuntime(5539): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationsherlocksearchviewmapv2/in.wptrafficanalyzer.locationsherlocksearchviewmapv2.SearchPlace}: java.lang.NullPointerException

07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.os.Looper.loop(Looper.java:137)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at dalvik.system.NativeStart.main(Native Method)
07-31 01:49:53.685: E/AndroidRuntime(5539): Caused by: java.lang.NullPointerException
07-31 01:49:53.685: E/AndroidRuntime(5539):     at in.w  ptrafficanalyzer.locationsherlocksearchviewmapv2.SearchPlace.handleIntent(SearchPlace.java:473)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at in.wptrafficanalyzer.locationsherlocksearchviewmapv2.SearchPlace.onCreate(SearchPlacer.java:129)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.Activity.performCreate(Activity.java:5104)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-31 01:49:53.685: E/AndroidRuntime(5539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

My SearchPlace Activity

    @Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.google_map_v2);

    handleIntent(getIntent());

}

private void handleIntent(Intent intent)
{
    if(intent.getAction().equals(Intent.ACTION_SEARCH))
    {
        doSearch(intent.getStringExtra(SearchManager.QUERY));
    }
    else if(intent.getAction().equals(Intent.ACTION_VIEW))
    {
        getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
    }
}

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

private void doSearch(String query)
{
    Bundle data = new Bundle();
    data.putString("query", query);
    getSupportLoaderManager().restartLoader(0, data, this);
}

private void getPlace(String query)
{
    Bundle data = new Bundle();
    data.putString("query", query);
    getSupportLoaderManager().restartLoader(1, data, this);
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle query)
{
    CursorLoader cLoader = null;
    if(arg0 == 0)
        cLoader = new CursorLoader(getBaseContext(),
                PlaceProvider.SEARCH_URI, null, null, new String[]
                { query.getString("query") }, null);
    else if(arg0 == 1)
        cLoader = new CursorLoader(getBaseContext(),
                PlaceProvider.DETAILS_URI, null, null, new String[]
                { query.getString("query") }, null);
    return cLoader;

}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c)
{
    showLocations(c);

}

@Override
public void onLoaderReset(Loader<Cursor> arg0)
{
    // TODO Auto-generated method stub
}

Can anyone help me plz ?

Thank you


Solution

  • Problem solved, modify the handleIntent() as

    if(Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
    String query = intent.getStringExtra(SearchManager.QUERY);
    doSearch(query);
    }
    else if(Intent.ACTION_VIEW.equals(intent.getAction()))
    {
    getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
    }