I am trying to populate a listview with an icon and a piece of text that is received from a class which has member variables for both of these items.
The app crashes each time the list view is revealed in the activity, and I can't see what is wrong with it. I'm sure the problem is within the custom array adapter, because I can initialise and view a standard listview no problem, but I can't see where this problem is lying.
The class with the info is defined below:
public class Pokemon {
// core info
String name;
String number;
int no;
int icon;
public Pokemon(String name, int n, int icon){
this.name = name;
this.number = Integer.toString(n);
this.no = n;
this.icon = icon;
}
And instantiated as an ArrayList below:
ArrayList<Pokemon> starters = new ArrayList<>();
starters.add(new Pokemon("Bulbasaur", 1, R.drawable.p1)
);
starters.add(new Pokemon("Ivysaur", 2, R.drawable.p2)
);
starters.add(new Pokemon("Venasaur", 3, R.drawable.p3)
);
starters.add(new Pokemon("Charmander", 4, R.drawable.p4)
);
starters.add(new Pokemon("Charmeleon", 5, R.drawable.p5)
);
starters.add(new Pokemon("Charizard", 6, R.drawable.p6)
);
starters.add(new Pokemon("Squirtle", 7, R.drawable.p7)
);
starters.add(new Pokemon("Wartortle", 8, R.drawable.p8)
);
starters.add(new Pokemon("Blastoise", 9, R.drawable.p9)
);
My custom adaptor is:
public class PokedexArrayAdaptor extends ArrayAdapter<Pokemon>{
Context context;
List<Pokemon> pokemonList;
int layoutResourceId;
public PokedexArrayAdaptor(Context context, int layoutResourceId, ArrayList<Pokemon> objects) {
super(context, layoutResourceId, objects);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.pokemonList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
PokemonHolder holder;
Pokemon pokemon = pokemonList.get(position);
if(rowView == null){
LayoutInflater vi;
vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = vi.inflate(layoutResourceId, null);
holder = new PokemonHolder();
holder.name = (TextView)findViewById(R.id.pokemonNameList);
holder.sprite = (ImageView)findViewById(R.id.pokemonIconList);
holder.desc = (TextView)findViewById(R.id.secondLine);
rowView.setTag(holder);
}
else holder = (PokemonHolder)rowView.getTag();
Pokemon pok = pokemonList.get(position);
holder.name.setText(pok.name);
holder.sprite.setImageResource(pokemon.icon);
holder.desc.setText(pok.number);
return rowView;
}
}
Within OnCreate I am calling and setting the adaptor with:
customAdaptor = new PokedexArrayAdaptor(this, R.layout.pokedex_entry, starters);
pokedexList = (ListView)findViewById(R.id.pokedexListView);
pokedexList.setAdapter(customAdaptor);
my ListView xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/pokemonIconList"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:paddingRight="40dp"
android:paddingLeft="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/p1" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp"
android:layout_toRightOf="@id/pokemonIconList"/>
<TextView
android:id="@+id/pokemonNameList"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp"
android:layout_toRightOf="@id/pokemonIconList"/>
And my xml for the activity is:
<?xml version="1.0" encoding="utf-8"?>
<dallasapps.shinyhunter.SlidingContainer
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFCDD2">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/pokedexListView">
</ListView>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainTrackScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="dallasapps.shinyhunter.Track"
android:background="#E0E0E0"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/sideBtn"
android:visibility="invisible"
android:background="#0000"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gen: ORAS"
android:id="@+id/genTxt"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/pokedexBtn"
android:background="@drawable/pokedex_xy_icon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="match_parent"
android:layout_marginTop="74dp"
android:layout_height="110dp"
android:scaleType="fitCenter"
android:layout_alignParentTop="true"
android:src="@drawable/p8"
android:id="@+id/trackedPokemon"
android:layout_below="@+id/pokedexBtn"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:text="#001 Bulbasaur"
android:id="@+id/pokedexNo"
android:gravity="center_vertical"
android:layout_below="@+id/trackedPokemon"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pokedexNo"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="50sp"
android:text="-"
android:id="@+id/minBtn"
android:background="#F44336"
android:layout_weight="1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/counterTxt"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Chance"
android:id="@+id/chance"
android:textAlignment="center"
android:layout_below="@+id/linearLayout"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="1/4096"
android:id="@+id/chanceTxt"
android:layout_below="@+id/chance"
android:layout_gravity="center"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="50sp"
android:text="+"
android:id="@+id/plusBtn"
android:background="#F44336"
android:layout_weight="1"
/>
</LinearLayout>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="-15dp"
android:scaleType="fitCenter"
android:background="#0000"
android:drawableBottom="@drawable/tallgrass"
android:text="Method"
android:id="@+id/methodBtn"
style="?android:attr/borderlessButtonStyle"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/trackedPokemon"
android:layout_alignEnd="@+id/trackedPokemon"/>
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="24dp"
android:text="Found"
android:layout_marginBottom="30dp"
android:id="@+id/foundBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:id="@+id/scTrackChk"
android:checked="false"
android:layout_alignBottom="@+id/foundBtn"
android:layout_alignLeft="@+id/methodBtn"
android:layout_alignStart="@+id/methodBtn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Shiny Charm"
android:id="@+id/scTrackText"
android:layout_below="@+id/scTrackChk"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/imageView"
android:src="@drawable/shiny_charm"
android:layout_above="@+id/scTrackChk"
android:layout_alignRight="@+id/scTrackChk"
android:layout_alignEnd="@+id/scTrackChk"/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/eggTrackImage"
android:src="@drawable/egg"
android:layout_above="@+id/mmTrackChk"
android:layout_alignRight="@+id/mmTrackChk"
android:layout_alignEnd="@+id/mmTrackChk"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:id="@+id/mmTrackChk"
android:checked="false"
android:layout_alignBaseline="@+id/scTrackChk"
android:layout_alignBottom="@+id/scTrackChk"
android:layout_alignRight="@+id/pokedexBtn"
android:layout_alignEnd="@+id/pokedexBtn"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Masuda"
android:id="@+id/masudaTrackTxt"
android:layout_alignTop="@+id/scTrackText"
android:layout_alignLeft="@+id/pokedexBtn"
android:layout_alignStart="@+id/pokedexBtn"/>
</RelativeLayout>
I can't seem to get the listview to populate at all. It works when I use the standard array adapter and a simple String[] list = {...}
to populate it, so I am confident it's a problem with my custom adapter, but I can't see how.
I have tried reducing the holder to only work on the text value and set the other values to constants across each list value, but that didn't work either.
Any ideas what is going wrong? I have followed a number of guides online trying different methods of overriding the ArrayAdapter, but none of them are working.
Any help is greatly appreciated.
EDIT: Crashlog:
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: FATAL EXCEPTION: main
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: Process: dallasapps.shinyhunter, PID: 16121
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at dallasapps.shinyhunter.Track$PokedexArrayAdaptor.getView(Track.java:98)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.AbsListView.obtainView(AbsListView.java:2346)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.ListView.makeAndAddView(ListView.java:1876)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.ListView.fillDown(ListView.java:702)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.ListView.fillFromTop(ListView.java:763)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.ListView.layoutChildren(ListView.java:1685)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.AbsListView.onLayout(AbsListView.java:2148)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at dallasapps.shinyhunter.SlidingContainer.onLayout(SlidingContainer.java:70)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2684)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.View.layout(View.java:16692)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5445)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2178)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1938)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1114)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6068)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.Choreographer.doCallbacks(Choreographer.java:670)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:606)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5539)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-09 10:21:56.377 16121-16121/dallasapps.shinyhunter D/AppTracker: App Event: crash
In getView method . You have to use rowView.findViewById. Not only findViewById. Otherwise it will get null pointer exception.