我正在尝试在网格视图中的图像下方添加文本。我收到空点异常错误。
下面是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#000080">
<imageview android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</imageview>
<textview android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#000000">
</textview>
</LinearLayout>
ImageAdapter 类具有以下代码:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image6, R.drawable.image3,
R.drawable.image5, R.drawable.image4
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
mInflater = LayoutInflater.from(c);
}
public ImageAdapter(Residential residential) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.grid_item,
parent,false);
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText("Item");
ImageView imageView = (ImageView)MyView.findViewById(R.id.grid_item_image);
//ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(190, 100));
}
return MyView;
}
}
它在下面的行抛出空异常错误:
convertView = mInflater.inflate(R.layout.grid_item,
parent,false);
我尝试了各种建议的选项,但都给出了相同的结果
LayoutInflater li = ((Activity) mContext).getLayoutInflater();
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
请建议如何解决这个问题。谢谢。