2013-01-10 2 views
3

У меня возникла проблема с созданием моего собственного CustomInfoWindow. получать исключение не знаю, где & почемуСоздание CustomInfoWindow в google map v2

это мой простой customInfoWindow класс

public class CustomInfoWindow implements InfoWindowAdapter { 

private LayoutInflater mInflater; 

public CustomInfoWindow(LayoutInflater inflater) { 
    this.mInflater=inflater; 

} 


@Override 
public View getInfoContents(Marker marker) { 
    View popup = mInflater.inflate(R.layout.info_window_layout, null); 
    TextView tv=(TextView)popup.findViewById(R.id.title); 
    tv.setText(marker.getTitle()); 
    tv=(TextView)popup.findViewById(R.id.address); 
    tv.setText(marker.getSnippet()); 

    return popup; 
} 

@Override 
public View getInfoWindow(Marker marker) { 
    View popup = mInflater.inflate(R.layout.info_window_layout, null); 
    TextView tv=(TextView)popup.findViewById(R.id.title); 
    tv.setText(marker.getTitle()); 
    tv=(TextView)popup.findViewById(R.id.address); 
    tv.setText(marker.getSnippet()); 

    return popup; 
} 

}

Я устанавливаю его здесь. (MainActivity)

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
      .getMap(); 
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
      Statics.LAT_TLV, Statics.LON_TLV), 13)); 

    CustomInfoWindow customInfoWindow = new CustomInfoWindow(getLayoutInflater()); 
    mMap.setInfoWindowAdapter(customInfoWindow); 

Я не реализовал метод onMarkerClick. на карту нагрузки в порядке с маркерами (около 40), но когда я нажимаю на одном из маркеров я получаю:

01-10 13:15:24.321: E/AndroidRuntime(21162): FATAL EXCEPTION: main 
01-10 13:15:24.321: E/AndroidRuntime(21162): java.lang.NullPointerException 
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.view.View.measure(View.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.y.i(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.y.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.w.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.bd.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.y.bw.b(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.y.bw.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dh.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.n.c(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dw.a(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.bd.c(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.a.dq.onSingleTapConfirmed(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.e.v.onSingleTapConfirmed(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at maps.e.j.handleMessage(Unknown Source) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.os.Handler.dispatchMessage(Handler.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.os.Looper.loop(Looper.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at android.app.ActivityThread.main(ActivityThread.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at java.lang.reflect.Method.invokeNative(Native Method) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at java.lang.reflect.Method.invoke(Method.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
01-10 13:15:24.321: E/AndroidRuntime(21162): at dalvik.system.NativeStart.main(Native Method) 

был бы признателен, если кто-то может помочь мне с этим, и если есть очень простой пример как создать свой собственный просмотр информации было бы замечательно. Огромное спасибо. Уди

+0

У меня точно такая же ошибка , если я использую getInfoContents, все в порядке, но если я попробую getInfoWindows, произойдет сбой приложений. – Ernest

ответ

12

Вы не должны быть переопределение как getInfoWindow() и getInfoContents(), по крайней мере там, где getInfoWindow() никогда не сможет вернуться null. Ваш getInfoContents() никогда не будет использоваться.

Если бы я должен был догадаться, ваша проблема кроется в вашем файле макета, но это всего лишь предположение.

Вот пример InfoWindowAdapter:

class PopupAdapter implements InfoWindowAdapter { 
    LayoutInflater inflater=null; 

    PopupAdapter(LayoutInflater inflater) { 
    this.inflater=inflater; 
    } 

    @Override 
    public View getInfoWindow(Marker marker) { 
    return(null); 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
    View popup=inflater.inflate(R.layout.popup, null); 

    TextView tv=(TextView)popup.findViewById(R.id.title); 

    tv.setText(marker.getTitle()); 
    tv=(TextView)popup.findViewById(R.id.snippet); 
    tv.setText(marker.getSnippet()); 

    return(popup); 
    } 
} 

Используя этот файл макета для InfoWindow:

<?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="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:padding="2dip" 
     android:src="@drawable/ic_launcher" 
     android:contentDescription="@string/icon"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="25sp" 
      android:textStyle="bold"/> 

     <TextView 
      android:id="@+id/snippet" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15sp"/> 
    </LinearLayout> 

</LinearLayout> 

Полный пример приложения можно найти по адресу: https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Popups

+0

Большое спасибо. отлично работает. Мне действительно не нужно реализовывать getInfoWindow, просто вернув null. thanks alot –

+0

Кажется, это здорово ... но как назвать это из MainActivity и применить его к каждому маркеру карты? – smartmouse

+0

@smartmouse: Это применимо к каждому маркеру карты, а 'PopupAdapter' применяется в« MainActivity », как вы можете видеть при изучении проекта, связанного с ответом. – CommonsWare