0

我想創建自己的信息窗口,但是當我進入地圖活動時,它崩潰(甚至在我看到地圖之前)。如何在android中爲google maps api v2創建自定義信息窗口?

這是我正在使用的代碼。有誰知道最新錯誤?

謝謝。

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 [email protected],*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 [email protected],*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 
+1

發佈來自logcat的堆棧跟蹤 – Karakuri

+1

我懷疑findFragmentById返回null,這會給你'getMap()'的調用帶來'NullPointerException'。您的活動XML不包含帶有MapFragment的''標記,因此該片段尚未附加到您的活動。是這樣嗎? – Karakuri

+0

好吧,我把痕跡。 – omega

回答

0

如果你的活動XML沒有<fragment>標籤,然後就沒有連接到你的活動,直到您添加片段他們自己使用FragmentManager和FragmentTransaction。這意味着findFragmentById()將返回null,然後您試圖在空對象上調用getMap()。這會導致堆棧跟蹤中顯示NullPointerException

你需要做兩件事情之一:

  1. 地圖片段添加到您的活動的XML佈局。如果您的活動將始終顯示地圖,那麼這是最簡單的解決方案。
  2. 在將MapFragment(或SupportMapFragment)附加到活動後,只調用findFragmentById()。
+0

我在聲明map之後通過移動地圖監聽器來修復它。但是我如何在佈局文件中實際編輯textview視圖?是否有一個oncreate事件?我試圖在getcontents函數中設置一個文本,但它不起作用。 (請參閱上面的編輯) – omega

+0

在InfoWindowAdapter中,將'getInfoContents()'內的代碼移動到'getInfoWindow()'。 – Karakuri

+0

我在getInfoContents()裏面保存了什麼?如果我只是返回null,那麼我得到一個透明的窗口。但至少textview正在改變。 – omega

相關問題