谷歌地图说“不幸的是,<app name =”“>已停止”</app>

时间:2012-07-31 13:57:36

标签: android google-maps

每当我尝试运行我的应用时,都会收到此错误:

unfortunately,  has stopped

来源:

package com.example.mapsapp;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.os.Bundle;
import android.view.Menu;


public class HichRideActivity extends MapActivity
{


    MapController mControl;
    GeoPoint geop;
    MapView mapV;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hich_ride);

        mapV= (MapView) findViewById(R.id.menu_settings);
        mapV.displayZoomControls(true);
        mapV.setBuiltInZoomControls(true);


        double lat= 40.8;
        double longi=-96.666;

        geop = new GeoPoint ((int)(lat * 1E6), (int)(longi * 1E6));
        mControl.animateTo(geop);
        mControl.setZoom(13);

    }


   @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
       getMenuInflater().inflate(R.menu.activity_hich_ride, menu);
        return true;
   }

    @Override
    protected boolean isRouteDisplayed()
    {
        // TODO Auto-generated method stub
        return false;
    }


}

这里是logcat,它说我有NullPointerException但我不知道哪个参数是null

logcat的:

07-31 13:40:46.344: E/Trace(1043): error opening trace file: No such file or directory (2)
07-31 13:40:47.134: D/AndroidRuntime(1043): Shutting down VM
07-31 13:40:47.134: W/dalvikvm(1043): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-31 13:40:47.194: D/dalvikvm(1043): GC_CONCURRENT freed 195K, 3% free 8273K/8519K, paused 80ms+5ms, total 166ms
07-31 13:40:47.194: D/dalvikvm(1043): WAIT_FOR_CONCURRENT_GC blocked 26ms
07-31 13:40:47.194: E/AndroidRuntime(1043): FATAL EXCEPTION: main
07-31 13:40:47.194: E/AndroidRuntime(1043): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapsapp/com.example.mapsapp.HichRideActivity}: java.lang.NullPointerException
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.os.Looper.loop(Looper.java:137)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at java.lang.reflect.Method.invoke(Method.java:511)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at dalvik.system.NativeStart.main(Native Method)
07-31 13:40:47.194: E/AndroidRuntime(1043): Caused by: java.lang.NullPointerException
07-31 13:40:47.194: E/AndroidRuntime(1043):     at com.example.mapsapp.HichRideActivity.onCreate(HichRideActivity.java:27)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.Activity.performCreate(Activity.java:5008)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-31 13:40:47.194: E/AndroidRuntime(1043):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-31 13:40:47.194: E/AndroidRuntime(1043):     ... 11 more
07-31 13:40:47.214: W/CursorWrapperInner(1043): Cursor finalized without prior close()
07-31 13:40:47.214: W/CursorWrapperInner(1043): Cursor finalized without prior close()

2 个答案:

答案 0 :(得分:1)

mapV= (MapView) findViewById(R.id.menu_settings);
                                  ^^^^^^^^^^^^^

这应该是MapView ID ,它应该在activity_hich_ride.xml

中声明

答案 1 :(得分:0)

这些行:mControl.animateTo(geop); mControl.setZoom(13); 是你的问题。您使用的是尚未创建的MapController,您已将其声明为类的顶部,但您从未实例化它 - 因此,在您声明MapView之后,请添加以下行: mControl = mapV.getController();。尝试一下,让我知道它是否有效!