Android Studio OnInfoWindowClickListener无法正常工作

时间:2014-06-25 14:31:53

标签: android map android-studio

我使用Android Studio并使用maps活动构建了一个新应用。

我创建了一个新类并实现了OnInfoWindowClickListener。以下是我的代码

public class testListener extends Activity实现OnInfoWindowClickListener {

public testListener(GoogleMap mMap) {

    float blue = BitmapDescriptorFactory.HUE_AZURE;
    float red = BitmapDescriptorFactory.HUE_RED;


    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-26.055984, 28.084833))
                    .title("pick me")
                    .icon(BitmapDescriptorFactory.defaultMarker(blue))
    );


    mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-26.058394, 28.078792))
                    .title("Hello World")
                    .icon(BitmapDescriptorFactory.defaultMarker(red))
    );
    mMap.setOnInfoWindowClickListener(this);

}

@Override
public void onInfoWindowClick(Marker marker) {
    try {
        Toast.makeText(testListener.this, "Click Info Window", Toast.LENGTH_LONG).show();
    } catch (Exception e){
        e.printStackTrace();
    }
}

}

为什么吐司消息不显示?申请以遗憾结束,已停止。

java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:93)
at android.widget.Toast.makeText(Toast.java:241)
at com.cinema.movies1.southAfrica.onInfoWindowClick(testListener.java:46)
at com.google.android.gms.maps.GoogleMap$10.e(Unknown Source)
at com.google.android.gms.maps.internal.f$a.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at fno.a(SourceFile:82)
at lxs.b(Unknown Source)
at ltq.g(Unknown Source)
at ltq.a(Unknown Source)
at lnm.b(Unknown Source)
at loh.onSingleTapConfirmed(Unknown Source)
at lbl.onSingleTapConfirmed(Unknown Source)
at lbn.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)   

尝试捕获异常输出:

W/System.err﹕ java.lang.NullPointerException
W/System.err﹕ at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
W/System.err﹕ at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
W/System.err﹕ at android.widget.Toast.<init>(Toast.java:93)
W/System.err﹕ at android.widget.Toast.makeText(Toast.java:241)
W/System.err﹕ at com.cinema.movies1.southAfrica.onInfoWindowClick(testListener.java:47)
W/System.err﹕ at com.google.android.gms.maps.GoogleMap$10.e(Unknown Source)
W/System.err﹕ at com.google.android.gms.maps.internal.f$a.onTransact(Unknown Source)
W/System.err﹕ at android.os.Binder.transact(Binder.java:361)
W/System.err﹕ at fno.a(SourceFile:82)
W/System.err﹕ at lxs.b(Unknown Source)
W/System.err﹕ at ltq.g(Unknown Source)
W/System.err﹕ at ltq.a(Unknown Source)
W/System.err﹕ at lnm.b(Unknown Source)
W/System.err﹕ at loh.onSingleTapConfirmed(Unknown Source)
W/System.err﹕ at lbl.onSingleTapConfirmed(Unknown Source)
W/System.err﹕ at lbn.handleMessage(Unknown Source)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您是否添加了此代码:

public class MapsActivity extends MapActivity {

    public void onCreate(Bundle savedInstanceState) {
        ...
        setUpMap();
    }

    private void setUpMap() {
        float blue = BitmapDescriptorFactory.HUE_AZURE;
        float red = BitmapDescriptorFactory.HUE_RED;

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(-26.055984, 28.084833))
                .title("pick me")
                .icon(BitmapDescriptorFactory.defaultMarker(blue))
        );

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(-26.058394, 28.078792))
                .title("Hello World")
                .icon(BitmapDescriptorFactory.defaultMarker(red))
        );

        mMap.setOnInfoWindowClickListener(this);
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        try {
            Toast.makeText(MapsActivity.this, "Click Info Window", Toast.LENGTH_LONG).show();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}