2013-05-14 97 views
0

我在開始應用程序時不斷收到此錯誤。此代碼以google api v1開頭,我一直試圖完全轉換爲V2。然而,當我運行它時,它會關閉應用程序。它基於一個在線教程,即時學習。 有什麼建議嗎?無法啓動活動?需要幫助

package -; 

import java.util.List; 
import android.content.Intent; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Window; 

import -.AddItemizedOverlay; 
import -.Place; 
import -.PlacesList; 
import -.R; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 

public class PlacesMapActivity extends FragmentActivity { 
    // Nearest places 
    PlacesList nearPlaces; 

    // Map view 
    MapFragment mapView; 

    // Map overlay items 
    List<Overlay> mapOverlays; 

    AddItemizedOverlay itemizedOverlay; 

    GeoPoint geoPoint; 
    // Map controllers 
    MapController mc; 

    double latitude; 
    double longitude; 
    OverlayItem overlayitem; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_places); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); 
     Intent i = getIntent(); 

     // Users current geo location 
     String user_latitude = i.getStringExtra("user_latitude"); 
     String user_longitude = i.getStringExtra("user_longitude"); 

     // Nearplaces list 
     nearPlaces = (PlacesList) i.getSerializableExtra("near_places"); 

     SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

     // Geopoint to place on map 
     geoPoint = new GeoPoint((int) (Double.parseDouble(user_latitude) * 1E6), 
       (int) (Double.parseDouble(user_longitude) * 1E6)); 

     // Drawable marker icon 
     Drawable drawable_user = this.getResources() 
       .getDrawable(R.drawable.mark_red); 

     itemizedOverlay = new AddItemizedOverlay(drawable_user, this); 

     // Map overlay item 
     overlayitem = new OverlayItem(geoPoint, "Your Location", 
       "That is you!"); 

     itemizedOverlay.addOverlay(overlayitem); 

     mapOverlays.add(itemizedOverlay); 
     itemizedOverlay.populateNow(); 

     // Drawable marker icon 
     Drawable drawable = this.getResources() 
       .getDrawable(R.drawable.mark_blue); 

     itemizedOverlay = new AddItemizedOverlay(drawable, this); 

     // These values are used to get map boundary area 
     // The area where you can see all the markers on screen 
     int minLat = Integer.MAX_VALUE; 
     int minLong = Integer.MAX_VALUE; 
     int maxLat = Integer.MIN_VALUE; 
     int maxLong = Integer.MIN_VALUE; 

     // check for null in case it is null 
     if (nearPlaces.results != null) { 
      // loop through all the places 
      for (Place place : nearPlaces.results) { 
       latitude = place.geometry.location.lat; // latitude 
       longitude = place.geometry.location.lng; // longitude 

       // Geopoint to place on map 
       geoPoint = new GeoPoint((int) (latitude * 1E6), 
         (int) (longitude * 1E6)); 

       // Map overlay item 
       overlayitem = new OverlayItem(geoPoint, place.name, 
         place.vicinity); 

       itemizedOverlay.addOverlay(overlayitem); 


       // calculating map boundary area 
       minLat = (int) Math.min(geoPoint.getLatitudeE6(), minLat); 
       minLong = (int) Math.min(geoPoint.getLongitudeE6(), minLong); 
       maxLat = (int) Math.max(geoPoint.getLatitudeE6(), maxLat); 
       maxLong = (int) Math.max(geoPoint.getLongitudeE6(), maxLong); 
      } 
      mapOverlays.add(itemizedOverlay); 

      // showing all overlay items 
      itemizedOverlay.populateNow(); 
     } 


    } 

} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
<fragment 
     android:id="@+id/map" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:name="com.google.android.gms.maps.SupportMapFragment" /> 
</LinearLayout> 

05-15 01:04:49.513: W/dalvikvm(12551): threadid=1: thread exiting with uncaught exception (group=0x40aac228) 
05-15 01:04:49.513: E/AndroidRuntime(12551): FATAL EXCEPTION: main 
05-15 01:04:49.513: E/AndroidRuntime(12551): java.lang.RuntimeException: Unable to start activity ComponentInfo{-/-.PlacesMapActivity}: java.lang.NullPointerException 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread.access$600(ActivityThread.java:139) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.os.Looper.loop(Looper.java:154) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread.main(ActivityThread.java:4944) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at java.lang.reflect.Method.invokeNative(Native Method) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at java.lang.reflect.Method.invoke(Method.java:511) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at dalvik.system.NativeStart.main(Native Method) 
05-15 01:04:49.513: E/AndroidRuntime(12551): Caused by: java.lang.NullPointerException 
05-15 01:04:49.513: E/AndroidRuntime(12551): at -.PlacesMapActivity.onCreate(PlacesMapActivity.java:74) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.Activity.performCreate(Activity.java:4531) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 
05-15 01:04:49.513: E/AndroidRuntime(12551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150) 
05-15 01:04:49.513: E/AndroidRuntime(12551): ... 11 more 
05-15 01:04:51.115: D/Process(12551): killProcess, pid=12551 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="-" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:resizeable="true" 
    android:anyDensity="true" 
    android:theme="@android:style/Theme.NoTitleBar" > 

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="17" /> 



    <permission 
     android:name="-.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="-.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" ></uses-permission> 

     <application 
      android:icon="@drawable/applogo12" 
      android:label="@string/app_name" 
      android:theme="@style/CustomTheme"> 
      <uses-library android:name="com.google.android.maps" /> 



     <activity 
      android:name="-.SplashActivity" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".HomeActivity" android:screenOrientation="portrait"></activity> 

     <activity android:name=".PlacesMapActivity" android:screenOrientation="portrait"></activity> 



     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="-" /> 
    </application> 
</manifest> 
+0

沒有堆棧跟蹤,任何人都很難幫助您。 – CommonsWare

+0

堆棧跟蹤從logcat – proJ

回答

1

在Java堆棧跟蹤,追捕到您的代碼引用,以確定事情會出錯。在這種情況下,那就是:

05-15 01:04:49.513: E/AndroidRuntime(12551): Caused by: java.lang.NullPointerException 
05-15 01:04:49.513: E/AndroidRuntime(12551): at -.PlacesMapActivity.onCreate(PlacesMapActivity.java:74) 

如果我們假定這行是行74:

mapOverlays.add(itemizedOverlay); 

那麼這個例外是有道理的,因爲你永遠不初始化mapOverlays

除此之外,大部分代碼都無法正常工作,因爲它試圖在Maps V2地圖上執行Maps V1工作。例如,Maps V2沒有OverlayItem實例,也沒有ItemizedOverlay , nor GeoPoint。您可能會考慮提供所有這些代碼,直到您爲其編寫Maps V2替代品,而不僅僅是崩潰。

+0

以上哦,好吧,你會知道任何允許Google地圖結果顯示在谷歌地圖上的教程嗎?類似於http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/ – proJ

+0

@AmyJennings:使用Maps V2標記。 – CommonsWare