Tuesday 15 April 2014

android - NullPointerException when inflating custom InfoWindowAdapter -


I am trying to remove a custom InfoWindowAdapter in a separate class file. I started with an internal class that works right:

  public class FoobarMapActivity SherlockFragmentActivity expanded {protected GoogleMap mMap; Personal Ultimate GoogleMap.InfoWindowAdapter mInfoWindowAdapter; Public FoobarMapActivity () {mInfoWindowAdapter = New GoogleMap.InfoWindowAdapter () {@Override Public View getInfoWindow (Marker Marker) {Scene Window = getLayoutInflater () increased (R.layout.custom_info_window, tap). Render (marker, window); Return window; } @ Override public see getInfoContents (marker marker) {return tap; } Private zero render (marker marker, see 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 ())); }}; }   

But as soon as I put the same code in a different class, I go to a NullPointerException when I increase the layout

  due to:. Com.example.foobar.activities.CustomInfoWindowAdapter on android.view.LayoutInflater.from (LayoutInflater.java:210) java.lang.NullPointerException & LT; Init & gt; (CustomInfoWindowAdapter Java: 20) com.example.foobar.activities.FoobarMapActivity on & lt; Init & gt; (FoobarMapActivity.java:107) java.lang.Class.newInstanceImpl (Native Method) at java.lang.Class.newInstance (in class .. Java: 1319) android.app.Instrumentation.newActivity (Instrumentation.java:1023)   

Here is the custom class for InfoWindowAdapter :

  on android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1887) Code> 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 custom-embedding operator applies GoogleMap. Info WindowAdapter {see protected mWindow; Public CustomInfoWindowAdapter (Reference Reference) {LayoutInflater inflater = LayoutInflater.from (reference); MWindow = inflater.inflate (R. Layout CUSTOM_INFA_Window, Faucet); } @ Override Public View getInfoWindow (Marker Marker) {Render (marker, mWindow); Return mWindow; @ Override public view getInfoContents (marker marker) {return tap; } Private zero render (marker marker, see 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 ())); }}   

Here I instatiate the custom adapter:

  Public category FoobarMapActivity extends the SherlockFragmentActivity {Private Final GoogleMap.InfoWindowAdapter mInfoWindowAdapter; Public FoobarMapActivity () {mInfoWindowAdapter = New CustomInfoWindowAdapter (getBaseContext ()); // line 107}    

please move this line: < Pre> mInfoWindowAdapter = New CustomInfoWindowAdapter (getBaseContext ());

with onCreate and replace getBaseContext () with this .

You are doing something against the platform onCreate is your constructor and you activity While working with , the Java constructor should not be used.

No comments:

Post a Comment