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

1 回答 1

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。例如,没有OverlayItemMaps V2 的实例,也没有 ItemizedOverlay , norGeoPoint`。您可能会考虑注释掉所有这些代码,直到您为它编写 Maps V2 替代品,而不仅仅是崩溃。

于 2013-05-15T00:22:24.633 回答