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
}