我找不到合适的解决方案。我想要的是在每个单元格中使用CardView渲染GridView。出于某种原因,我收到此错误:片段类上的 java.lang.NullPointerException ,在 gridView.setAdapter(适配器)的行上; 有人可以解释一下这是什么问题,我该如何解决?
活性
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布局
<?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>
布局活动
<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"/>
更新 它现在正在工作!,由于某种原因Gradle导致问题,根本没有NullPointerException问题,我不得不清理项目3次然后重新启动Android Studio然后重新编译,然后重新同步Gradle和另一轮清理,最后渲染工作。投诉也在第21行,即gridView.setAdapter ....所以,我必须删除注释和额外的空间,以便验证行号是否为#21,并在缩小类并离开之后总共19行,抱怨仍然说那是第21行。我记得我过去几周所做的唯一更新是Gradle。
有关我在2016年5月使用Gradle 2.10和Android Studio 2.1.2版本#AI-143-2915827的信息。