I can't find the right solution. What I want is to render a GridView with CardView in each cell. For some reason I am getting this error: java.lang.NullPointerException on the fragment class, on the line that says gridView.setAdapter(adapter); Can some one please explain what is the issue and how can I fix it?
ACTIVITY
public class VoteActivity extends Activity {
private static final String CLASS_TAG = "VoteActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
Log.e("CLASS_TAG", CLASS_TAG);
setContentView(R.layout.activity_vote);
if (savedInstanceState == null) {
VoteFragment voteFragment = new VoteFragment();
voteFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, voteFragment).commit();
}
}
}
FRAGMENT
public class VoteFragment extends Fragment {
VoteAdapter adapter;
GridView gridView;
public VoteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_vote, container, false);
gridView = (GridView) v.findViewById(R.id.gridviewFragment);
gridView.setDrawSelectorOnTop(true);
adapter = new VoteAdapter(getActivity().getApplicationContext());
gridView.setAdapter(adapter);
return v;
}
}
ADAPTER
public class VoteAdapter extends BaseAdapter {
private Context mContext;
public VoteAdapter(Context context) {
this.mContext = context;
}
@Override
public int getCount() {
return CandidateGrid.ITEMS.length;
}
@Override
public CandidateGrid getItem(int position) {
return CandidateGrid.ITEMS[position];
}
@Override
public long getItemId(int position) {
return getItem(position).getCandidatePicture();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.vote_gridview, parent, false);
}
ImageView itemImage =
(ImageView) convertView.findViewById(R.id.item_image);
TextView itemTitle =
(TextView) convertView.findViewById(R.id.item_title);
TextView itemDetail =
(TextView) convertView.findViewById(R.id.item_detail);
final CandidateGrid item = getItem(position);
itemImage.setImageResource(item.getCandidatePicture());
itemTitle.setText(item.getCandidateName());
itemDetail.setText(item.getCandidateExtra());
return convertView;
}
}
CLASS TO FEED ADAPTER
public class CandidateGrid {
private int mCandidatePicture;
private String mCandidateName;
private String mCandidateExtra;
public CandidateGrid(int candidatePicture, String candidateName, String candidateExtra) {
this.mCandidatePicture = candidatePicture;
this.mCandidateName = candidateName;
this.mCandidateExtra = candidateExtra;
}
public int getCandidatePicture() {
return mCandidatePicture;
}
public String getCandidateName() {
return mCandidateName;
}
public String getCandidateExtra() {
return mCandidateExtra;
}
public static CandidateGrid[] ITEMS = {
new CandidateGrid(R.drawable.candidate1, "Candidate 1 name", "Candidate 1 Extra"),
new CandidateGrid(R.drawable.candidate2, "Candidate 2 name", "Candidate 2 Extra"),
new CandidateGrid(R.drawable.candidate3, "Candidate 3 name", "Candidate 3 Extra"),
new CandidateGrid(R.drawable.candidate4, "Candidate 4 name", "Candidate 4 Extra"),
new CandidateGrid(R.drawable.candidate5, "Candidate 5 name", "Candidate 5 Extra"),
new CandidateGrid(R.drawable.candidate6, "Candidate 6 name", "Candidate 6 Extra"),
new CandidateGrid(R.drawable.candidate7, "Candidate 7 name", "Candidate 7 Extra"),
new CandidateGrid(R.drawable.candidate8, "Candidate 8 name", "Candidate 8 Extra"),
new CandidateGrid(R.drawable.candidate1, "Candidate 9 name", "Candidate 9 Extra"),
new CandidateGrid(R.drawable.candidate2, "Candidate 10 name", "Candidate 10 Extra"),
new CandidateGrid(R.drawable.candidate3, "Candidate 11 name", "Candidate 11 Extra"),
new CandidateGrid(R.drawable.candidate4, "Candidate 12 name", "Candidate 12 Extra"),
new CandidateGrid(R.drawable.candidate5, "Candidate 13 name", "Candidate 13 Extra"),
new CandidateGrid(R.drawable.candidate6, "Candidate 14 name", "Candidate 14 Extra"),
new CandidateGrid(R.drawable.candidate7, "Candidate 15 name", "Candidate 15 Extra"),
new CandidateGrid(R.drawable.candidate8, "Candidate 16 name", "Candidate 16 Extra"),
new CandidateGrid(R.drawable.candidate1, "Candidate 17 name", "Candidate 17 Extra"),
new CandidateGrid(R.drawable.candidate2, "Candidate 18 name", "Candidate 18 Extra"),
new CandidateGrid(R.drawable.candidate3, "Candidate 19 name", "Candidate 19 Extra"),
new CandidateGrid(R.drawable.candidate4, "Candidate 20 name", "Candidate 20 Extra"),
new CandidateGrid(R.drawable.candidate5, "Candidate 21 name", "Candidate 21 Extra"),
new CandidateGrid(R.drawable.candidate6, "Candidate 22 name", "Candidate 22 Extra"),
new CandidateGrid(R.drawable.candidate7, "Candidate 23 name", "Candidate 23 Extra"),
new CandidateGrid(R.drawable.candidate8, "Candidate 24 name", "Candidate 24 Extra"),
};
public CandidateGrid getItem(int id) {
for (CandidateGrid item: ITEMS) {
if (item.getCandidatePicture() == id) {
return item;
}
}
return null;
}
}
FRAGMENT LAYOUT
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragments.VoteFragment">
<!-- A GridView with some commonly used attributes -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridviewFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="6"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth"
android:gravity="center"
/>
</FrameLayout>
CARDVIEW LAYOUT USED FOR ADAPTER
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:id="@+id/card_view"
android:layout_margin="6dp"
card_view:cardBackgroundColor="#FAFAFA"
card_view:cardCornerRadius="12dp"
card_view:cardElevation="6dp"
card_view:contentPadding="0dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/item_image"
android:padding="0dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_title"
android:layout_below="@+id/item_image"
android:textSize="20sp"
android:textColor="@android:color/black"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_detail"
android:layout_below="@+id/item_title"
android:textColor="@color/cardview_dark_background"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
LAYOUT FOR ACTIVITY
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"/>
UPDATE It's working now!, for some reason Gradle is causing the problem, there is not NullPointerException issue at all, I had to clean up the project 3 times and then relaunch Android Studio and then recompile, and then re-sync Gradle and another clean up round, and finally the rendering is working. Also the complain was at the line #21 which is the gridView.setAdapter.... So, I had to remove comments and extra space in order to validate if the line number was the #21, and after shrinking the class and leaving just 19 lines in total, the complain was still saying that was the #21 line. What I recall the only update I did the last couple of weeks was for Gradle.
For information I am using Gradle 2.10 and Android Studio 2.1.2 build #AI-143-2915827 on May 2016.