hy!
I always get a NullPointerEx in my code. The list size is 7 so this can’t be null. The customlistime exists. I have no idea where do i get the Exception. In my other programm this code worked.
Please help
Calling:
Log.e("SP",String.valueOf(list.size())); //List size is 7
ca = new CustomAdapter(this, R.layout.customlistitem, list); //global var
lv.setAdapter(ca); //Error Occurs here
CustomAdaptor:
public class CustomAdapter extends ArrayAdapter<SPEntry>{
private ArrayList<SPEntry> entries;
public CustomAdapter(Context context, int textViewResourceId,ArrayList<SPEntry> objects) {
super(context, textViewResourceId, objects);
this.entries = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater li = (LayoutInflater)CustomAdapter.this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = li.inflate(R.layout.customlistitem,null);
holder.date = (TextView) convertView.findViewById(R.id.item_date);
holder.lesson = (TextView) convertView.findViewById(R.id.item_lesson);
holder.teacher = (TextView) convertView.findViewById(R.id.item_teacher);
holder.image = (ImageView) convertView.findViewById(R.id.item_picture);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.date.setText(entries.get(position).date);
holder.teacher.setText(entries.get(position).teacher);
holder.lesson.setText(entries.get(position).lesson);
Bitmap bt = Bitmap.createScaledBitmap(entries.get(position).picture, 48, 48, false);
holder.image.setImageBitmap(bt);
return convertView;
}
}
Viewholder:
public class ViewHolder {
public ImageView image;
public TextView date;
public TextView lesson;
public TextView teacher;
ViewHolder()
{
}
}
ErrorCode:
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.korn.supplierplan/com.korn.supplierplan.view.LVEntries}: java.lang.NullPointerException
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.os.Looper.loop(Looper.java:123)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at java.lang.reflect.Method.invoke(Method.java:521)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at dalvik.system.NativeStart.main(Native Method)
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): Caused by: java.lang.NullPointerException
10-17 20:49:39.481: ERROR/AndroidRuntime(2767): at com.korn.supplierplan.view.LVEntries.onCreate(LVEntries.java:30)
Picture from ca in the debugger:
It’s not
CustomAdapterproblem. It’s yourListViewobjectlvproblem, clearly it is not initialized or not inflated from layout.In your layout XML file add a
ListViewand add this before setCustomAdaptertoListViewEDIT: updated code, try to clean project and run it again!