Android SearchView小部件仅适用于启动器活动



我现在面临一个非常奇怪的错误。基本上,该应用程序可以使用SearchView小部件搜索Google Places API,并在Google Maps Android API V2中显示结果,SearchView小部件位于操作栏中。我参考了本教程:http://wptrafficanalyzer.in/blog/android-searchview-widget-with-google-places-api-using-actionbarsherlock-library/

当可搜索的活动(我称之为SearchPlace)是主活动和启动器活动时,该应用程序运行良好。然而,如果我试图从另一个活动中进行此活动,应用程序就会崩溃。我得到了由handleIntent()引起的NullPointerException。我在下面附上了清单文件的一部分。SearchPlace是我可搜索活动的名称。

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

错误日志附在下面。似乎是我的handleIntent()导致了错误。

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)

我的SearchPlace活动

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

有人能帮我吗?

感谢

问题已解决,将handleIntent()修改为

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));
}

相关内容

  • 没有找到相关文章

最新更新