使用CardView的RecyclerView不起作用

时间:2015-02-26 21:27:02

标签: java android xml adapter

我真的很抱歉这样一个抽象的问题,但我在这里失去了理智。这是我的RecyclerView活动代码。 请告诉我我错过了什么,因为我是Android的新手,我已经尝试了4天才能找到CardView。 错误是

02-26 13:17:36.570    1138-1138/com.parse.starter E/CrashReporting﹕ ParseCrashReporting caught a NullPointerException exception for com.parse.starter. Building report.
02-26 13:17:36.580    1138-1138/com.parse.starter E/CrashReporting﹕ Handling exception for crash
    java.lang.NullPointerException
            at com.misha.adaptor.ContactAdapter.onBindViewHolder(ContactAdapter.java:31)
            at com.misha.adaptor.ContactAdapter.onBindViewHolder(ContactAdapter.java:14)

主要活动类

package com.parse.starter;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.misha.adaptor.ContactAdapter;
import com.misha.to.ContactInfo;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;


public class CardActivity extends Activity {
    String column;
    TextView name;
    String result;
    List<ContactInfo> resultContacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);
//        getContacts()
        List<ContactInfo> list = new ArrayList<>();
        ContactInfo ci = new ContactInfo();
        ci.name = "Mihail";
        ci.gender = "male";
        ci.email = "test@gmail.com";
        ci.phone = "4152346153";
        list.add(ci);
        RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setLayoutManager(llm);
        ContactAdapter adapter = new ContactAdapter(list);
        recList.setAdapter(adapter);



//        name = (TextView) findViewById(R.id.txtName);
//        getMessage("fevK9QPFUW", "Message");


    }

    public void getMessage(String key, String columnName) {
        this.column = columnName;
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Messages");
        query.getInBackground(key, new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    name.setText(object.getString(column).toString());
                } else {
                    result = "did not work";
                }
            }
        });
    }

    public List<ContactInfo> getContacts() {
        resultContacts = new ArrayList<>();
        ParseQuery<ParseObject> contacts = ParseQuery.getQuery("Messages");
        List<ParseQuery<ParseObject>> queries = new ArrayList<>();
        queries.add(contacts);
        ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
        mainQuery.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> results, ParseException e) {
                if (e == null) {
                    for (ParseObject obj : results) {
                        String s = obj.getString("Message");
                        ContactInfo ci = new ContactInfo();
                        ci.name = s;
                        ci.email = "email";
                        ci.phone = "phone";
                        ci.gender = "gender";
                        resultContacts.add(ci);
                    }
                }
            }
        });
        return resultContacts;

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_card, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

适配器

package com.misha.adaptor;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.misha.to.ContactInfo;
import com.parse.starter.R;

import java.util.List;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private List<ContactInfo> contactList;

    public ContactAdapter(List<ContactInfo> contactList) {
        this.contactList = contactList;
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
        ContactInfo ci = contactList.get(i);
        System.out.println("The ContactInfo = " + ci);
        contactViewHolder.vName.setText(ci.name);
        contactViewHolder.vPhone.setText(ci.phone);
        contactViewHolder.vEmail.setText(ci.email);
        contactViewHolder.vGender.setText(ci.gender);
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.activity_card, viewGroup, false);

        return new ContactViewHolder(itemView);
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        protected TextView vName;
        protected TextView vPhone;
        protected TextView vEmail;
        protected TextView vGender;

        public ContactViewHolder(View v) {
            super(v);
            vName = (TextView) v.findViewById(R.id.txtName);
            vPhone = (TextView) v.findViewById(R.id.txtPhone);
            vEmail = (TextView) v.findViewById(R.id.txtEmail);
            vGender = (TextView) v.findViewById(R.id.txtGender);
        }
    }

}

以下是我的选择:

activity_card.xml

<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".CardActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

card_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/txtName"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@color/bkg_card"
            android:text="Name"
            android:gravity="center_vertical"
            android:textColor="@android:color/white"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/txtPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Phone"
            android:gravity="center_vertical"
            android:textSize="10dp"
            android:layout_below="@id/txtName"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp" />

        <TextView
            android:id="@+id/txtEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email"
            android:gravity="center_vertical"
            android:textSize="10dp"
            android:layout_below="@id/txtPhone"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp" />

        <TextView
            android:id="@+id/txtGender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gender"
            android:textSize="10dp"
            android:layout_marginTop="10dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="150dp"
            android:layout_alignBaseline="@id/txtEmail" />


    </RelativeLayout>


</android.support.v7.widget.CardView>

2 个答案:

答案 0 :(得分:0)

contactViewHolder.vName在您的示例中为空。

您需要在ContactViewHolder构造函数中调用ContactAdapter的构造函数。

答案 1 :(得分:0)

搞定了

@Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.card_layout, viewGroup, false);

        return new ContactViewHolder(itemView);
    }

将R.layout.card_layout设置为R.layout.my_activity