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