HY!
我总是在我的代码中获得NullPointerEx。列表大小为7,因此不能为空。 customlistime存在。我不知道我在哪里得到例外。在我的其他程序中,这段代码有效。
请帮忙
调用
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()
{
}
}
错误码:
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)
调试器中来自ca的图片:
答案 0 :(得分:0)
确保您的ListView变量(lv)已初始化。
答案 1 :(得分:0)
这不是CustomAdapter
问题。这是您的ListView
对象lv
问题,很明显它没有初始化或没有从布局中膨胀。
在您的布局XML文件中添加ListView
并在将CustomAdapter
设置为ListView
ListView lv = (ListView)findViewById(R.id.lv_sp);
ca = new CustomAdapter(this, R.layout.customlistitem, list);
lv.setAdapter(ca);
编辑:更新代码,尝试清理项目并再次运行!