我将图像从mysql数据库填充到gridView,用户使用复选框从中选择匹配症状的图像。当图像小于10时,一切正常,但当gridview有超过10个图像时,当用户按下继续按钮时问题出现它在Logcat中给出了一个空指针引用
Gridview with image from database
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.findViewById(int)' on a null object reference
at com.banmart.www.intergratedpestmanagement.ImageTest.CustomAdapterTwo$3.onClick(CustomAdapterTwo.java:120)
这是我的 CustomAdapterTwo.java
images = new ArrayList<>();
mAdapter = new ImageAdapter(this, images);
gridView.setAdapter(mAdapter);
proceed.setOnClickListener(new View.OnClickListener() {
String outputStrArr;
@Override
public void onClick(View v) {
int count = gridView.getAdapter().getCount();
ArrayList<String> selectedItems = new ArrayList<String>();
for(int i = 0; i < count; i++) {
LinearLayout linearLayout = (LinearLayout) gridView.getChildAt(i);
checkbox = (CheckBox) linearLayout.findViewById(R.id.checkBox1);
if(checkbox.isChecked()){
outputStrArr =checkbox.getTag().toString();
selectedItems.add(outputStrArr);
String [] strArr = new String[selectedItems.size()];
for (int j= 0; j < selectedItems.size(); j++) {
strArr[j] = selectedItems.get(j);
}
Intent intent = new Intent(getApplicationContext(),
ResultActivity.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", strArr);
// Add the bundle to the intent.
intent.putExtras(b);
// start the ResultActivity
startActivity(intent);
finish();
Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());
// Toast.makeText(CustomAdapterTwo.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
//selectedItems.add(checkbox.getTag());
}
}
}
});
ImageAdapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridtwo, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
viewHolder.Chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
viewHolder.Chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
images.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView1, viewHolder.textView);
convertView.setTag(R.id.imageView1, viewHolder.imageView);
convertView.setTag(R.id.checkBox1, viewHolder.Chkbox);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
ImagesLib image = images.get(position);
viewHolder.textView.setText(image.getAltText());
Glide.with(mContext).load(image.getImageUrl())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(viewHolder.imageView);
viewHolder.Chkbox.setTag(image.getId());
viewHolder.Chkbox.setTag(position);
viewHolder.Chkbox.setChecked(images.get(position).isSelected());
return convertView;
}
grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginRight="10px"
>
</ImageView>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</LinearLayout>
grid_gallery.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#050"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:text="Check the symptoms on the Banana Plant "
android:layout_span="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1">
<GridView
android:id="@+id/gridView1"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit" >
</GridView>
</LinearLayout>
<View
android:layout_height="1dip"
android:background="#CCCCCC" />
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:weightSum="3">
<Button
android:id="@+id/btnCheckAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Check All" />
<Button
android:id="@+id/btnClearAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Clear All" />
<Button
android:id="@+id/btnGetItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#070"
android:textColor="#fff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Continue >>" />
</LinearLayout>
</TableLayout>
答案 0 :(得分:0)
此调用的linearlayout为空
LinearLayout linearLayout = (LinearLayout) gridView.getChildAt(i);
而不是调用getChildAt
,而是必须使用此方法
public View getViewByPosition(int pos, GridView mGridView) {
final int firstListItemPosition = mGridView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + mGridView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return mGridView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return mGridView.getChildAt(childIndex);
}
}