我在活动布局上有一个自定义ListView(由bauerca进行DragSortListView),当我开始活动时,它会抛出NullPointerException并关闭应用程序,但是当我下次启动此活动时 - 它可以正常工作。如何删除此错误。 登录
06-05 14:47:13.218: E/AndroidRuntime(498): FATAL EXCEPTION: main
06-05 14:47:13.218: E/AndroidRuntime(498): java.lang.NullPointerException
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.ViewGroup.addView(ViewGroup.java:1821)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.ViewGroup.addView(ViewGroup.java:1808)
06-05 14:47:13.218: E/AndroidRuntime(498): at com.mobeta.android.dslv.DragSortListView$AdapterWrapper.getView(DragSortListView.java:739)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.ListView.fillDown(ListView.java:670)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.ListView.fillFromTop(ListView.java:727)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.ListView.layoutChildren(ListView.java:1584)
06-05 14:47:13.218: E/AndroidRuntime(498): at com.mobeta.android.dslv.DragSortListView.layoutChildren(DragSortListView.java:2136)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.View.layout(View.java:7175)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.View.layout(View.java:7175)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.View.layout(View.java:7175)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.View.layout(View.java:7175)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.os.Looper.loop(Looper.java:123)
06-05 14:47:13.218: E/AndroidRuntime(498): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 14:47:13.218: E/AndroidRuntime(498): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 14:47:13.218: E/AndroidRuntime(498): at java.lang.reflect.Method.invoke(Method.java:507)
06-05 14:47:13.218: E/AndroidRuntime(498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 14:47:13.218: E/AndroidRuntime(498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 14:47:13.218: E/AndroidRuntime(498): at dalvik.system.NativeStart.main(Native Method)
适配器
private class OrderAdapter extends ArrayAdapter<Order>{
public OrderAdapter(List<Order> list) {
super(MainActivity.this, R.layout.order_list_item,R.id.address,list);
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView item=(TextView) v.findViewById(R.id.address);
TextView subitem=(TextView) v.findViewById(R.id.client);
item.setTextColor(Color.WHITE);
subitem.setTextColor(Color.WHITE);
item.setText(getItem(position).order_address);
subitem.setText(getItem(position).order_client);
TextView pos = (TextView) v.findViewById(R.id.pos_number);
pos.setText(String.valueOf(getItem(position).position_number));
ImageView state = (ImageView) v.findViewById(R.id.checkItem);
state.setVisibility(View.VISIBLE);
if (getItem(position).order_status == 3)
state.setBackgroundResource(drawable.check_cancelled);
if (getItem(position).order_status == 2)
state.setBackgroundResource(drawable.check_checked);
if (getItem(position).order_status == 0)
state.setBackgroundResource(drawable.check_nb);
if (getItem(position).order_status == 1)
{
state.setVisibility(View.GONE);
}
return v;
初始化
c_orders = new ArrayList<Order>();
adapter = new OrderAdapter(c_orders);
myList=(DragSortListView)findViewById(R.id.dsvlist);
myList.setAdapter(adapter);
myList.setDropListener(onDrop);
myList.setRemoveListener(onRemove);
myList.setOnItemClickListener(listener);
答案 0 :(得分:0)
而不是打电话
View v = super.getView(position, convertView, parent);
自己充气。
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view v = inflater.inflate(R.layout.your_layout_name, parent, false);
.
.
.
.
.
注意:您应该阅读教程以了解“适配器如何工作?”和“提高listview性能的最佳方法是什么?”