无法开始活动?需要帮助

时间:2013-05-14 23:58:15

标签: android google-maps

启动应用时,我一直收到此错误消息。此代码以谷歌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> 

1 个答案:

答案 0 :(得分: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替换,而不是仅仅崩溃。