我想更新在ui线程上运行的listview适配器。
这是我的代码:
@Override
public void addItem(final String data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
list.setAdapter(new ListAdapter(MainActivity.this, data, width, height));
}
});
}
现在我的问题是这段代码总是引发异常,实际上我并不知道为什么或是什么原因导致例外......
以下是例外情况:
12-17 14:48:13.241: E/AndroidRuntime(26289): FATAL EXCEPTION: main
12-17 14:48:13.241: E/AndroidRuntime(26289): java.lang.NullPointerException
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.ListView.setupChild(ListView.java:1797)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.ListView.makeAndAddView(ListView.java:1775)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.ListView.fillDown(ListView.java:672)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.ListView.fillFromTop(ListView.java:732)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.ListView.layoutChildren(ListView.java:1625)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.View.layout(View.java:11282)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewGroup.layout(ViewGroup.java:4224)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1514)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2467)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.os.Looper.loop(Looper.java:137)
12-17 14:48:13.241: E/AndroidRuntime(26289): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-17 14:48:13.241: E/AndroidRuntime(26289): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 14:48:13.241: E/AndroidRuntime(26289): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 14:48:13.241: E/AndroidRuntime(26289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-17 14:48:13.241: E/AndroidRuntime(26289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-17 14:48:13.241: E/AndroidRuntime(26289): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我认为你的适配器的getView()是问题
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
if (tf == null) {
tf = new FontsProperties(parent.getContext()).getNormalFont();
}
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.aminities_item, parent, false);
ChildHolder holder = new ChildHolder();
holder.chkBox = (CheckBox) view.findViewById(R.id.chkBox);
view.setTag(holder);
}
ChildHolder holder = (ChildHolder) view.getTag();
holder.txtTitle.setText(items.get(position).getItem().getText(parent.getContext()));
holder.chkBox.setChecked(items.get(position).isSelected());
holder.linParent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
items.get(position).setSelected(!items.get(position).isSelected());
notifyDataSetChanged();
}
});
return view;