当我按下checkAll按钮时,它会在我的自定义ListView中显示NullpointerException
。
问题在于,如果屏幕上有6个或7个项目,根据屏幕大小而没有问题,则检查所有工作项目的大小超过屏幕大小,然后提供NullPointerException
。
我已将我的代码放在下面:
// ///////////////////// checkAll / ClearAll // ///////////////////////////////////////
btnCheckAll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int count =lvHiddenApp.getAdapter().getCount();
Log.i("NoOfApp", "" + count);
if (state) {
for (int i = 0; i < count; i++) {
RelativeLayout itemLayout = (RelativeLayout) lvHiddenApp
.getChildAt(i); // Find
CheckBox checkbox = (CheckBox) itemLayout
.findViewById(R.id.cbHideAppCheck);
checkbox.setChecked(true);
btnCheckAll.setText("UncheckAll");
state = false;
}
} else {
for (int i2 = 0; i2 < count; i2++) {
RelativeLayout itemLayout = (RelativeLayout) lvHiddenApp
.getChildAt(i2); // Find
CheckBox checkbox = (CheckBox) itemLayout
.findViewById(R.id.cbHideAppCheck);
checkbox.setChecked(false);
btnCheckAll.setText("checkAll");
state = true;
}
}
}
});
// ////////////////////////////////////////////////////////////////////////////////////////////////////
logcat的:
E/AndroidRuntime(2420): FATAL EXCEPTION: main
E/AndroidRuntime(2420): java.lang.NullPointerException
E/AndroidRuntime(2420):at com.example.settings.HiddenAppList$1.onClick(HiddenAppList.java:92)
E/AndroidRuntime(2420):at com.rey.material.widget.RippleManager.run(RippleManager.java:83)
E/AndroidRuntime(2420):at com.rey.material.widget.RippleManager.onClick(RippleManager.java:77)
E/AndroidRuntime(2420):at android.view.View.performClick(View.java:4240)
E/AndroidRuntime(2420):at android.view.View$PerformClick.run(View.java:17721)
E/AndroidRuntime(2420):at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(2420):at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(2420):at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(2420):at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(2420):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2420):at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(2420):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(2420):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-09 12:17:48.083: E/AndroidRuntime(2420):at dalvik.system.NativeStart.main(Native Method)
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ivHideAppIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="@+id/img"
android:src="@drawable/ic_launcher" />
<com.rey.material.widget.TextView
android:id="@+id/tvHideAppText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ivHideAppIcon"
android:padding="4dp"
android:text="@string/hidden_app" />
<CheckBox
android:id="@+id/cbHideAppCheck"
style="@style/CheckBoxDrawable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="false" />
适配器文件:
public class HideAppListviewAdapter extends BaseAdapter {
public HideAppListviewAdapter() {
// TODO Auto-generated constructor stub
}
String i;
ArrayList<String> state = new ArrayList<String>();;
Context context;
private ArrayList<Category> items;
private LayoutInflater mInflater;
public class ViewHolder {
public ImageView imageView;
public TextView textTitle;
public CheckBox cb;
public Button btnitem;
}
public HideAppListviewAdapter(Context context, ArrayList<Category> cat) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
items = cat;
Log.i("itemsizepela", "" + items.size());
}
public ArrayList<Category> getItems() {
return items;
}
public void setItems(ArrayList<Category> items) {
this.items = items;
}
@Override
public int getCount() {
if (items != null) {
return items.size();
}
return 0;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public Category getItem(int position) {
if (items != null && position >= 0 && position < getCount()) {
return items.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
if (items != null && position >= 0 && position < getCount()) {
return items.get(position).id;
}
return 0;
}
public void setItemsList(ArrayList<Category> locations) {
this.items = locations;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
final ViewHolder viewHolder;
final Category gridItems = getItem(position);
if (view == null) {
view = mInflater.inflate(R.layout.hidden_app_list_item, parent,
false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view
.findViewById(R.id.ivHideAppIcon);
viewHolder.textTitle = (TextView) view
.findViewById(R.id.tvHideAppText);
viewHolder.cb = (CheckBox) view.findViewById(R.id.cbHideAppCheck);
view.setTag(viewHolder);
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbHideAppCheck);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(gridItems.box);
} else {
viewHolder = (ViewHolder) view.getTag();
}
setCatImage(position, viewHolder, gridItems.name, gridItems.icon);
return view;
}
@SuppressLint("NewApi")
private void setCatImage(int pos, ViewHolder viewHolder, String catTitle,
Drawable icon) {
viewHolder.imageView.setImageDrawable(icon);
viewHolder.textTitle.setText(catTitle);
}
ArrayList<Category> getBox() {
ArrayList<Category> box = new ArrayList<Category>();
for (Category p : items) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getItem((Integer) buttonView.getTag()).box = isChecked;
}
};
}
答案 0 :(得分:0)
您从lvHiddenApp.getChildAt(i)
获得空返回值。 ListViews仅保留屏幕上项目的膨胀视图。当项目离开屏幕时,视图将重新用于新项目,并且只需填充新数据(在适配器中)。
您必须在适配器中保持项目状态(如选择),而不是UI组件。
确保适配器实施正确初始化所有视图&#39;状态,或者我想你会得到一些有趣的问题(尝试检查长列表中的单个项目,然后向下滚动,你就会明白我的意思)。
答案 1 :(得分:-1)
在将适配器设置为列表视图之前,您是否尝试单击按钮?否则请输入完整代码或logcat日志。