我有一个片段和活动,里面有gridview。它首先启动片段的活动。并且片段内有按钮将打开或启动gridview活动。我的代码中没有红线,所以我运行该项目。但是当我打开片段上的按钮时,它会返回错误或崩溃。
它说
"无法启动活动ComponentInfo(EpicList):java.lang.NullPointetException:尝试调用虚方法' void android.widget.GridView.setAdapter(android.widget.ListAdapter)'在null对象引用"
这是所有代码
//片段
BundleEpic.java
public class BundleEpic extends Fragment {
public BundleEpic() {
// Required empty public constructor
}
private ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_bundleepic, container, false);
imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), EpicList.class);
startActivity(intent);
}
});
return view;
}
}
//使用Gridview的BasicActivity
EpicList.java
public class EpicList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bundleepic);
GridView gridView = (GridView)findViewById(R.id.gridview);
List<EpicListItem> epicListItems = getEpicListItemList();
EpicListAdapter epicListAdapter = new EpicListAdapter(EpicList.this, epicListItems);
gridView.setAdapter(epicListAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
final Intent intent;
switch (position) {
case 0:
intent = new Intent(EpicList.this, MainActivity.class);
break;
default:
intent = new Intent(EpicList.this, MainActivity.class);
break;
}
EpicList.this.startActivity(intent);
}
});
}
private List<EpicListItem> getEpicListItemList() {
EpicListItem epicListItem = null;
List<EpicListItem> items = new ArrayList<>();
items.add(new EpicListItem(R.drawable.border_epic));
return items;
}
}
EpicListAdapter
public class EpicListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<EpicListItem> epicListItems;
private Context context;
public EpicListAdapter(Context context, List<EpicListItem> customizeListView) {
this.context = context;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
epicListItems = customizeListView;
}
@Override
public int getCount() {
return epicListItems.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHoler;
if (convertView == null) {
listViewHoler = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.activity_epic_item, parent, false);
listViewHoler.imageInListView = (ImageView)convertView.findViewById(R.id.image);
convertView.setTag(listViewHoler);
} else {
listViewHoler = (ViewHolder)convertView.getTag();
}
listViewHoler.imageInListView.setImageResource(epicListItems.get(position).getImageResource());
return convertView;
}
static class ViewHolder {
ImageView imageInListView;
}
}
EpicListItem.java
public class EpicListItem {
private int imageResource;
public EpicListItem(int imageResource) {
this.imageResource = imageResource;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
}
activity_epic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".EpicList"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="5dp" />
</RelativeLayout>
activity_epic_item.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">
<RelativeLayout
android:layout_width="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:id="@+id/image"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="3dp"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:0)
如错误所示,它无法将适配器设置为null。
这可能是因为您尝试使用findViewById(int id)
查找视图,其中布局尚未膨胀。