我正在使用gridview,我想使用适配器在gridview中显示图像和文本,但我无法理解为什么它会崩溃。 GridAdapter.java.and中存在错误 我不知道为什么这段代码会给我一个带有gridview的NullPointerException
以下是我的代码:
GridAdapter.java
public class GridAdapter extends BaseAdapter {
private Context mContext;
private final String[] web;
private final int[] Imageid;
public GridAdapter(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_item, null);
TextView textView = (TextView) grid.findViewById(R.id.product_name);
ImageView imageView = (ImageView)grid.findViewById(R.id.pic);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
GridActivity.java
public class GridActivity extends Activity {
GridView simpleGrid;
String[] web = {"Google", "Github", "Instagram", "Facebook"} ;
int[] imageId = {R.drawable.product_imag,R.drawable.product_imag,R.drawable.product_imag,R.drawable.product_imag};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
simpleGrid = (GridView) findViewById(R.id.gv);
GridAdapter adapter = new GridAdapter(GridActivity.this, web, imageId);
simpleGrid.setAdapter(adapter);
simpleGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(GridActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
编辑Griditem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e6e6e6"
android:orientation="vertical"
android:padding="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:padding="1.5dp">
<ImageView
android:id="@+id/pic"
android:layout_width="150dp"
android:layout_height="250dp"
android:layout_gravity="top|center"
android:padding="5dp"
android:src="@drawable/product_imag"/>
<view
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#e6e6e6"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/product_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:text="New text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/product_price"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="New Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/product_other"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/product_name"
android:text="New Text" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
activity_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Vertical space between grid items -->
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="2"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:stretchMode="columnWidth"
android:horizontalSpacing="10dp"
android:id="@+id/gv"
android:verticalSpacing="50dp"></GridView>
</LinearLayout>
这是我的LogCat
FATAL EXCEPTION: main
Process: android.mendonca.averdapp, PID: 6640
android.view.InflateException: Binary XML file line #24: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:761)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.mendonca.averdapp.activity.GridAdapter.getView(GridAdapter.java:55)
at android.widget.AbsListView.obtainView(AbsListView.java:2427)
at android.widget.GridView.onMeasure(GridView.java:1065)
at android.view.View.measure(View.java:19881)
答案 0 :(得分:1)
尝试在第24行的grid_item.xml中使用View元素而不是view。
答案 1 :(得分:0)
Maby您需要覆盖方法
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return web[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return Imageid[position];
}