我正在从一个txt文件中读取一个列表然后在一个显示列表按钮上我能够显示列表,列表项,图像和使用自定义网格视图的复选框。现在使用另一个按钮单击我试图获取已检查项目的位置,以便我可以使用这些选定的项目。我的代码给了我一个空指针异常错误。 view.getCheckedItemPositions();
MainActivity.java
if(v.getId()==R.id.toast)
{
Toast.makeText(this,"positions" + adapter.getCheckedItemPositions(view), Toast.LENGTH_SHORT).show();
}
}
public class CustomAdapter extends BaseAdapter {
@Override
public View getView(int position, View view, ViewGroup parent) {
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view = new View(getApplicationContext());
TextView text=new TextView(getApplicationContext());
text.setTextColor(Color.BLACK);
text.setText(content.get(position));
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setChecked(!checkBox.isChecked());
checkBox.setChecked(true);
layout.addView(checkBox);
layout.addView(text);
return layout;
}
public List<Integer> getCheckedItemPositions(GridView view){
SparseBooleanArray checked = new SparseBooleanArray();
checked = view.getCheckedItemPositions();
List<Integer> positions = new ArrayList<>();
int checksize=checked.size();
for(int i=0; i<checksize; i++) {
if (checked.valueAt(i)) {
positions.add(checked.keyAt(i));
}
}
return positions;
}
}
}
Activity_main.xml
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gridView"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/toast"
android:layout_alignParentTop="true"
android:layout_alignEnd="@+id/gridView"
android:layout_marginEnd="49dp" />
</RelativeLayout>
logcat的
---------崩溃的开始 09-18 18:31:49.144 31751-31751 / com.example.administrator.myapplication E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.example.administrator.myapplication,PID:31751 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int android.util.SparseBooleanArray.size()' at com.example.administrator.myapplication.MainActivity $ CustomAdapter.getCheckedItemPositions(MainActivity.java:158) 在com.example.administrator.myapplication.MainActivity.onClick(MainActivity.java:96) 在android.view.View.performClick(View.java:4780) 在android.view.View $ PerformClick.run(View.java:19866) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 09-18 18:32:26.135 31751-31758 / com.example.administrator.myapplication W / art:暂停所有线程:102.748ms 09-18 18:32:27.078 31751-31758 / com.example.administrator.myapplication W / art:暂停所有线程:78.288ms