I have a fragment and activity with gridview inside it. It start the fragment acticity first. And there is button inside fragment that will open or start the gridview activity. There is no red line in my code, so i run the project. But when i open the button on fragment it return error or crash.
It says
"Unable to start activity ComponentInfo(EpicList): java.lang.NullPointetException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference"
.
Here is all the code
//Fragment
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;
}
}
//BasicActivity with Gridview
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>