在膨胀自定义InfoWindowAdapter时出现NullPointerException

时间:2013-06-22 11:54:00

标签: android nullpointerexception google-maps-android-api-2 layout-inflater android-inflate

我正在尝试将自定义InfoWindowAdapter提取到单独的类文件中。我开始使用一个完美的内部课程:

public class FoobarMapActivity extends SherlockFragmentActivity {

    protected GoogleMap mMap;
    private final GoogleMap.InfoWindowAdapter mInfoWindowAdapter;

    public FoobarMapActivity() {
        mInfoWindowAdapter = new GoogleMap.InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker marker) {
                View window = getLayoutInflater().inflate(R.layout.custom_info_window, null);
                render(marker, window);
                return window;
            }

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

            private void render(Marker marker, View view) {
                ((ImageView) view.findViewById(R.id.badge)).setImageResource(0);
                TextView titleTextView = ((TextView) view.findViewById(R.id.title));
                TextView snippetTextView = ((TextView) view.findViewById(R.id.snippet));
                titleTextView.setText(StringHelper.setTextOrEmpty(marker.getTitle()));
                snippetTextView.setText(StringHelper.setTextOrEmpty(marker.getSnippet()));
            }

        };
    }

但是当我将相同的代码放入一个单独的类时,当我给布局充气时,我会遇到NullPointerException

Caused by: java.lang.NullPointerException
  at android.view.LayoutInflater.from(LayoutInflater.java:210)
  at com.example.foobar.activities.CustomInfoWindowAdapter.<init>(CustomInfoWindowAdapter.java:20)
  at com.example.foobar.activities.FoobarMapActivity.<init>(FoobarMapActivity.java:107)
  at java.lang.Class.newInstanceImpl(Native Method)
  at java.lang.Class.newInstance(Class.java:1319)
  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1887)

以下是InfoWindowAdapter的自定义类:

package com.example.foobar.activities;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;

import com.example.foobar.R;
import com.example.foobar.utils.StringHelper;

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

    protected View mWindow;

    public CustomInfoWindowAdapter(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mWindow = inflater.inflate(R.layout.custom_info_window, null);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        render(marker, mWindow);
        return mWindow;


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

    private void render(Marker marker, View view) {
        ((ImageView) view.findViewById(R.id.badge)).setImageResource(0);
        TextView titleTextView = ((TextView) view.findViewById(R.id.title));
        TextView snippetTextView = ((TextView) view.findViewById(R.id.snippet));
        titleTextView.setText(StringHelper.setTextOrEmpty(marker.getTitle()));
        snippetTextView.setText(StringHelper.setTextOrEmpty(marker.getSnippet()));
    }

}

这里我实例化自定义适配器:

public class FoobarMapActivity extends SherlockFragmentActivity {

    private final GoogleMap.InfoWindowAdapter mInfoWindowAdapter;

    public FoobarMapActivity() {
        mInfoWindowAdapter = new CustomInfoWindowAdapter(getBaseContext()); // line 107
    }

3 个答案:

答案 0 :(得分:2)

请移动此行:

mInfoWindowAdapter = new CustomInfoWindowAdapter(getBaseContext());

进入onCreate并将getBaseContext()替换为this

你正在对平台做些什么。 onCreate是您的构造函数,在使用Activities时不应使用Java构造函数。

答案 1 :(得分:0)

我认为你在渲染功能中有NullReference 尝试添加空检查:

private void render(Marker marker, View view) {
        if (view == null || marker == null)
          return;
        ((ImageView) view.findViewById(R.id.badge)).setImageResource(0);
        TextView titleTextView = ((TextView) view.findViewById(R.id.title));
        TextView snippetTextView = ((TextView) view.findViewById(R.id.snippet));
        if (titleTextView != null)
          titleTextView.setText(StringHelper.setTextOrEmpty(marker.getTitle()));
        if (snippetTextView!= null)
          snippetTextView.setText(StringHelper.setTextOrEmpty(marker.getSnippet()));
    }

编辑:

尝试以另一种方式获取LayoutInflater:

mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE);

并检查上下文是否为空。

EDITED2:

尝试

mInfoWindowAdapter = new CustomInfoWindowAdapter(this);

答案 2 :(得分:0)

尝试使用getApplicationContext()或您的活动的上下文而不是getBaseContext()Context context = this;为您的活动制作上下文。