I try to create an activity to choose categories but my listview throws an exception after first element is created. I want to create the list item as: picture on left and name on the right. with default entries.
public class categoryAdapter extends BaseAdapter{
String [] categories;
Context context;
int [] imageId;
//List<Integer> categoriesChosen = new ArrayList<Integer>();
private static LayoutInflater inflater=null;
public categoryAdapter(Context mainActivity, String[] categories, int[] images) {
// TODO Auto-generated constructor stub
this.categories = categories;
imageId = images;
context=mainActivity;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return categories.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
Log.e("Holder Created", categories[position]);
View rowView;
Log.e("View Created", categories[position]);
rowView = inflater.inflate(R.layout.choose_category, null);
Log.e("Inflater inflated", categories[position]);
holder.tv=(TextView) rowView.findViewById(R.id.category_text_view);
Log.e("view tv", categories[position]);
holder.img=(ImageView) rowView.findViewById(R.id.category_image);
Log.e("view img", categories[position]);
holder.tv.setText(categories[position]);
Log.e("tv set", categories[position]);
holder.img.setImageResource(imageId[position]);
Log.e("img set", categories[position]);
Log.e("Category created", categories[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + categories[position], Toast.LENGTH_LONG).show();
//categoriesChosen.add(position);
}
});
Log.e("onclick set", categories[position]);
//return rowView;
return null;
}
}
Here is my Activity
//Categories and images for those categories
String[] categories = {
"Animals", "Art", "Entertainment", "Style", "Family", "DIY", "Food", "Science & Tech", "Music & Dac", "Movies % TV"
};
int[] images = {R.drawable.index_background, R.drawable.index_background, R.drawable.index_background,
R.drawable.index_background, R.drawable.index_background, R.drawable.index_background, R.drawable.index_background,
R.drawable.index_background, R.drawable.index_background, R.drawable.index_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_category);
String message;
for(int i=0; i<categories.length;i++){
message = Integer.toString(images[i]);
Log.e("Category and Its int", message );
}
ListView listView = (ListView) findViewById(R.id.choose_category_listview);
categoryAdapter adapter = new categoryAdapter(getApplicationContext(), categories, images);
listView.setAdapter(adapter);
And the error I get ... With my logs ...
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash E/Category and Its int﹕ 2130837566
06-22 21:15:20.886 19553-19553/io.splash.www.splash D/Activity﹕ performCreate Call secproduct feature valuefalse
06-22 21:15:20.886 19553-19553/io.splash.www.splash D/Activity﹕ performCreate Call debug elastic valuetrue
06-22 21:15:20.896 19553-19553/io.splash.www.splash D/MultiPhoneWindow﹕ refreshUI state=1
06-22 21:15:20.896 19553-19553/io.splash.www.splash D/MultiPhoneWindow﹕ performUpdateVisibility, same visibility false
06-22 21:15:20.896 19553-19553/io.splash.www.splash D/MultiPhoneWindow﹕ MinimizeAnimator::removeWindow
06-22 21:15:20.901 19553-19553/io.splash.www.splash E/Holder Created﹕ Animals
06-22 21:15:20.901 19553-19553/io.splash.www.splash E/View Created﹕ Animals
06-22 21:15:20.906 19553-19553/io.splash.www.splash E/Inflater inflated﹕ Animals
06-22 21:15:20.906 19553-19553/io.splash.www.splash E/view tv﹕ Animals
06-22 21:15:20.906 19553-19553/io.splash.www.splash E/view img﹕ Animals
06-22 21:15:20.906 19553-19553/io.splash.www.splash E/tv set﹕ Animals
06-22 21:15:20.906 19553-19553/io.splash.www.splash V/BitmapFactory﹕ DecodeImagePath(decodeResourceStream3) : res/drawable/index_background.jpg
06-22 21:15:21.151 19553-19553/io.splash.www.splash E/img set﹕ Animals
06-22 21:15:21.151 19553-19553/io.splash.www.splash E/Category created﹕ Animals
06-22 21:15:21.151 19553-19553/io.splash.www.splash E/onclick set﹕ Animals
06-22 21:15:21.151 19553-19553/io.splash.www.splash D/AndroidRuntime﹕ Shutting down VM
06-22 21:15:21.156 19553-19553/io.splash.www.splash E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.splash.www.splash, PID: 19553
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.widget.ListView.measureScrapChild(ListView.java:1210)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
at android.widget.ListView.onMeasure(ListView.java:1202)
at android.view.View.measure(View.java:18580)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18580)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
at android.view.View.measure(View.java:18580)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Why do I get this error ?
Here's how you get this error:
should be: