0

我正在尝试创建自己的信息窗口,但是当我进入地图活动时,它崩溃了(在我看到地图之前)。

这是我正在使用的代码。有谁知道出了什么问题?

谢谢。

public class ActivityMap extends FragmentActivity implements Restartable { 

    GoogleMap map = null;
    public static String ID;
    public HashMap<String, String> markerID = new HashMap<String, String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);




        final Context context = this;
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        map.setInfoWindowAdapter(new InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker arg0) {
                View v = getLayoutInflater().inflate(R.layout.activity_infowindow, null);

                ((TextView)v.findViewById(R.id.textView1)).setText("test");

                return v;
            }
        });

        /*
        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                if (CommonFunctions.isNetworkAvailable(context)) {
                    CommonFunctions.RegisterForRestart(ActivityMap.this, "ActivityMap");

                    String clickedID = markerID.get(marker.getId());
                    Intent myIntent = new Intent(context, ActivityProfile.class);
                    myIntent.putExtra("ID", clickedID);
                    startActivity(myIntent);
                } else {
                    Toast.makeText(context, getResources().getString(R.string.no_internet), Toast.LENGTH_LONG).show();
                }
            }
        });
        */

        Bundle b = getIntent().getExtras();
        ID = b.getString("ID");

        ShowData();
    }


    @Override
    public void onDestroy() {
        map.clear();
        super.onDestroy();
    }

    public void CloseActivity(View view) {
        finish();
    }

    @Override
    public void ShowData() {
        map.clear();
        AsyncLoadMapLocations profileTask = new AsyncLoadMapLocations(ActivityMap.this);
        profileTask.execute(ID);
    }

    public void AddMarker(String id, String location, String firstname, String lastname, String picurl) {
        LatLng latlong = AddressToLatLong(location);
        if (latlong != null) {
            //Marker newmarker = map.addMarker(new MarkerOptions().position(latlong).title(firstname + " " + lastname).snippet(location).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
            Marker newmarker = map.addMarker(new MarkerOptions().position(latlong).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
            markerID.put(newmarker.getId(), id);
        }
    }

    public LatLng AddressToLatLong(String strAddress) {
        Geocoder coder = new Geocoder(this);
        List<Address> address;

        try {
            address = coder.getFromLocationName(strAddress, 1);
            if (address == null) {
                return null;
            }
            Address location = address.get(0);
            return new LatLng(location.getLatitude(), location.getLongitude());
        } catch (Exception ex) {
            Log.d("ADDRESS", ex.getMessage());
        }
        return null;
    }
}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

堆栈跟踪 :

08-04 23:24:12.283: D/dalvikvm(31636): Set heap target utilization to 768/1024 (0.750000)
08-04 23:24:12.283: D/ActivityThread(31636): setTargetHeapUtilization:0.75
08-04 23:24:12.283: D/ActivityThread(31636): setTargetHeapIdealFree:2097152
08-04 23:24:12.513: D/libEGL(31636): loaded /system/lib/egl/libEGL_adreno200.so
08-04 23:24:12.513: D/libEGL(31636): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
08-04 23:24:12.513: D/libEGL(31636): loaded /system/lib/egl/libGLESv2_adreno200.so
08-04 23:24:12.543: I/Adreno200-EGLSUB(31636): <ConfigWindowMatch:2087>: Format RGBA_8888.
08-04 23:24:12.553: E/(31636): <s3dReadConfigFile:75>: Can't open file for reading
08-04 23:24:12.563: E/(31636): <s3dReadConfigFile:75>: Can't open file for reading
08-04 23:24:12.563: D/OpenGLRenderer(31636): Enabling debug mode 0
08-04 23:24:13.864: I/Adreno200-EGLSUB(31636): <ConfigWindowMatch:2087>: Format RGBA_8888.
08-04 23:24:13.904: E/SensorManager(31636): thread start
08-04 23:24:13.904: D/SensorManager(31636): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*b*:e.8
08-04 23:24:14.065: E/SpannableStringBuilder(31636): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-04 23:24:14.065: E/SpannableStringBuilder(31636): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-04 23:24:14.785: D/ProgressBar(31636): setProgress = 0
08-04 23:24:14.785: D/ProgressBar(31636): setProgress = 0, fromUser = false
08-04 23:24:14.785: D/ProgressBar(31636): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
08-04 23:24:14.815: W/ResourceType(31636): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)
08-04 23:24:14.855: I/Adreno200-EGLSUB(31636): <ConfigWindowMatch:2087>: Format RGBA_8888.
08-04 23:24:15.346: D/SensorManager(31636): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*b*:e.8
08-04 23:24:15.346: D/Sensors(31636): Remain listener = Sending .. normal delay 200ms
08-04 23:24:15.346: I/Sensors(31636): sendDelay --- 200000000
08-04 23:24:15.346: D/SensorManager(31636): JNI - sendDelay
08-04 23:24:15.346: I/SensorManager(31636): Set normal delay = true
08-04 23:24:15.446: I/Adreno200-EGLSUB(31636): <ConfigWindowMatch:2087>: Format RGBA_8888.
08-04 23:24:15.496: W/IInputConnectionWrapper(31636): getSelectedText on inactive InputConnection
08-04 23:24:15.516: W/IInputConnectionWrapper(31636): setComposingText on inactive InputConnection
08-04 23:24:17.418: D/dalvikvm(31636): GC_CONCURRENT freed 372K, 15% free 9945K/11651K, paused 16ms+22ms, total 86ms
08-04 23:24:17.418: D/dalvikvm(31636): WAIT_FOR_CONCURRENT_GC blocked 11ms
08-04 23:24:17.618: D/dalvikvm(31636): GC_CONCURRENT freed 381K, 15% free 10001K/11719K, paused 12ms+2ms, total 54ms
08-04 23:24:17.798: D/dalvikvm(31636): GC_CONCURRENT freed 249K, 14% free 10245K/11847K, paused 12ms+14ms, total 65ms
08-04 23:24:17.858: D/AbsListView(31636): Get MotionRecognitionManager
08-04 23:24:17.948: D/dalvikvm(31636): GC_CONCURRENT freed 235K, 13% free 10535K/12103K, paused 2ms+17ms, total 53ms
08-04 23:24:17.948: D/dalvikvm(31636): WAIT_FOR_CONCURRENT_GC blocked 2ms
08-04 23:24:17.958: D/dalvikvm(31636): WAIT_FOR_CONCURRENT_GC blocked 6ms
08-04 23:24:17.988: D/AndroidRuntime(31636): Shutting down VM
08-04 23:24:17.998: W/dalvikvm(31636): threadid=1: thread exiting with uncaught exception (group=0x40f82360)
08-04 23:24:18.008: E/AndroidRuntime(31636): FATAL EXCEPTION: main
08-04 23:24:18.008: E/AndroidRuntime(31636): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idsconnect/com.example.idsconnect.ActivityMap}: java.lang.NullPointerException
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.os.Looper.loop(Looper.java:137)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread.main(ActivityThread.java:4950)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at java.lang.reflect.Method.invokeNative(Native Method)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at java.lang.reflect.Method.invoke(Method.java:511)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at dalvik.system.NativeStart.main(Native Method)
08-04 23:24:18.008: E/AndroidRuntime(31636): Caused by: java.lang.NullPointerException
08-04 23:24:18.008: E/AndroidRuntime(31636):    at com.example.idsconnect.ActivityMap.onCreate(ActivityMap.java:39)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.Activity.performCreate(Activity.java:5177)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-04 23:24:18.008: E/AndroidRuntime(31636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-04 23:24:18.008: E/AndroidRuntime(31636):    ... 11 more
08-04 23:24:18.239: D/dalvikvm(31636): GC_CONCURRENT freed 282K, 14% free 10700K/12359K, paused 12ms+23ms, total 69ms
08-04 23:24:26.537: I/Process(31636): Sending signal. PID: 31636 SIG: 9
4

1 回答 1

0

如果您的活动 XML 没有<fragment>标签,那么在您使用 FragmentManager 和 FragmentTransaction 自己添加之前,不会有任何片段附加到您的活动。这意味着findFragmentById()将返回 null,然后您尝试调用getMap()null 对象。这会导致NullPointerException堆栈跟踪中显示。

您需要做以下两件事之一:

  1. 将地图片段添加到活动的 xml 布局中。如果您的活动将始终显示地图,那么这是最简单的解决方案。
  2. 仅在将 MapFragment(或 SupportMapFragment)附加到 Activity 后才调用 findFragmentById()。
于 2013-08-05T03:33:06.383 回答