0

我正在尝试将自定义提取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
    }
4

3 回答 3

2

请移动这一行:

mInfoWindowAdapter = new CustomInfoWindowAdapter(getBaseContext());

进入onCreate并替换getBaseContext()this.

你正在对平台做一些事情。onCreate是您的构造函数,在使用Activities.

于 2013-06-22T14:18:25.800 回答
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);

并检查上下文不为空。

编辑2:

尝试

mInfoWindowAdapter = new CustomInfoWindowAdapter(this);
于 2013-06-22T11:58:30.117 回答
0

尝试使用getApplicationContext()Activity 的上下文而不是getBaseContext(). Context context = this;使上下文对您的活动全局化。

于 2013-06-22T12:47:10.877 回答