0

我現在正面臨一個非常奇怪的錯誤。基本上,該應用可以使用SearchView小部件搜索Google Places API,並在Google Maps Android API V2中顯示結果,並將searchview小部件放置在操作欄中。我參考了本教程: http://wptrafficanalyzer.in/blog/android-searchview-widget-with-google-places-api-using-actionbarsherlock-library/Android SearchView小工具僅適用於啓動器活動

當可搜索活動(我將其命名爲SearchPlace)是主要和啓動器活動時,該應用正常工作。但是,如果我嘗試從其他活動中進行此活動,則應用程序崩潰。我得到了由handleIntent()引起的NullPointerException。 我附上了SearchPlace下面的Manifest文件的一部分,是我的可搜索活動的名稱。

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

誰能幫我PLZ?

謝謝

回答

0

問題解決了,修改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)); 
}