6

我找不到正确的解决方案。我想要的是在每个单元格中使用 CardView 渲染一个 GridView。出于某种原因,我收到了这个错误:片段类上的java.lang.NullPointerException ,在gridView.setAdapter(adapter);的行上。有人可以解释一下是什么问题,我该如何解决?

活动

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();
        }


    }

}

分段

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;
    }

}

适配器

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;
    }
}

类到饲料适配器

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;
    }
}

片段布局

<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 build #AI-143-2915827。

4

0 回答 0