1

我目前正在编码一个谷歌地图应用程序。我有一个带有地图片段和 MapActivity 的 XML 文件。如果用户单击地图,我尝试添加标记,但应用程序崩溃。

我试图解决这个问题几个小时,但我只是没有任何线索。

最好的问候迈克尔

package de.arvidg.exampleactionbartabs;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.location.Location;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
//import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MapFrag extends MapActivity implements OnMapClickListener{

    private GoogleMap myMap;

    private MapFragment mapView;

    private MyItemizedOverlay myItemizedOverlay = null;

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



        GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        mapView=((MapFragment) getFragmentManager().findFragmentById(
                R.id.map));

        map.setMyLocationEnabled(true);

        map.setTrafficEnabled(true);

        map.addMarker(new MarkerOptions()
        .position(new LatLng(40.417325, 40.417325))
        .title("Hello!"));

        map.setOnMapClickListener(this);
    }   



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


    @Override
    public void onMapClick(LatLng point) {
        myMap.addMarker(new MarkerOptions().position(point).title("jojoa"));


    }

}

日志输出:

03-08 16:20:38.500: I/Adreno200-EGL(10598): Build Date: 07/31/12 Tue
03-08 16:20:38.500: I/Adreno200-EGL(10598): Local Branch: 
03-08 16:20:38.500: I/Adreno200-EGL(10598): Remote Branch: quic/master
03-08 16:20:38.500: I/Adreno200-EGL(10598): Local Patches: NONE
03-08 16:20:38.500: I/Adreno200-EGL(10598): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 +  NOTHING
03-08 16:20:38.540: D/OpenGLRenderer(10598): Enabling debug mode 0
03-08 16:20:38.550: I/MapActivity(10598): Handling network change notification:CONNECTED
03-08 16:20:38.550: E/MapActivity(10598): Couldn't get connection factory client
03-08 16:20:38.920: D/dalvikvm(10598): GC_FOR_ALLOC freed 3301K, 46% free 6748K/12288K, paused 29ms, total 29ms
03-08 16:20:38.930: W/CursorWrapperInner(10598): Cursor finalized without prior close()
03-08 16:20:40.050: D/AndroidRuntime(10598): Shutting down VM
03-08 16:20:40.050: W/dalvikvm(10598): threadid=1: thread exiting with uncaught exception (group=0x40d71300)
03-08 16:20:40.060: E/AndroidRuntime(10598): FATAL EXCEPTION: main
03-08 16:20:40.060: E/AndroidRuntime(10598): java.lang.NullPointerException
03-08 16:20:40.060: E/AndroidRuntime(10598):    at de.arvidg.exampleactionbartabs.MapFrag.onMapClick(MapFrag.java:75)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at com.google.android.gms.maps.GoogleMap$6.onMapClick(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at com.google.android.gms.internal.t$a.onTransact(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at android.os.Binder.transact(Binder.java:326)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at com.google.android.gms.maps.internal.IOnMapClickListener$Stub$Proxy.onMapClick(IOnMapClickListener.java:93)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at maps.i.s.b(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at maps.y.v.c(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at maps.y.bf.onSingleTapConfirmed(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at maps.d.v.onSingleTapConfirmed(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at maps.d.j.handleMessage(Unknown Source)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at android.os.Looper.loop(Looper.java:137)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at android.app.ActivityThread.main(ActivityThread.java:4978)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at java.lang.reflect.Method.invokeNative(Native Method)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at java.lang.reflect.Method.invoke(Method.java:511)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-08 16:20:40.060: E/AndroidRuntime(10598):    at dalvik.system.NativeStart.main(Native Method)
03-08 16:20:41.240: I/Process(10598): Sending signal. PID: 10598 SIG: 9
4

1 回答 1

2

GoogleMap map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap()正在引发 NullPointerException,当它被单击时。

确保使用的 id 与 XML 文件中定义的相同。

于 2013-03-08T15:32:52.653 回答