DataAdapter中的空指针异常

时间:2017-03-22 09:50:38

标签: android android-layout recycler-adapter

我想要一个RecyclerView for Multple&不同的布局,我无法解决此错误。

错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ImageView.setImageResource(int)'

错误代码

((ViewHolder1)holder).mIdView.setImageResource(((ViewHolder1)holder).mItem.id)

DataAdapter的

package krishidarshan;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import krishidarshan.dummy.DummyContent.DummyItem;

/**
* Created by Abhishek on 19-03-2017.
*/

public class DataAdapter extends   RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<DummyItem> mValues;
int type;


public DataAdapter(List<DummyItem> items, int layoutType) {
    mValues = items;
    type = layoutType;
}

@Override
public int getItemViewType(int position) {
    return type;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == 1) {
        View view1 = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view, parent, false);
        return new ViewHolder1(view1);
    } else {
        View view2 = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_crops, parent, false);
        return new ViewHolder2(view2);
    }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof ViewHolder1) {
        ((ViewHolder1) holder).mItem = mValues.get(position);
        ((ViewHolder1) holder).mIdView.setImageResource(((ViewHolder1) holder).mItem.id);
        ((ViewHolder1) holder).mIdView.setImageResource(((ViewHolder1) holder).mItem.id);
        ((ViewHolder1) holder).mContentView.setText(((ViewHolder1) holder).mItem.content);

    } else if (holder instanceof ViewHolder2) {
        ((ViewHolder2) holder).mItem = mValues.get(position);
        ((ViewHolder2) holder).mIdView.setImageResource(((ViewHolder2) holder).mItem.id);
        ((ViewHolder2) holder).mContentView.setText(((ViewHolder2) holder).mItem.content);
        ((ViewHolder2) holder).mContentDetailView.setText(((ViewHolder2) holder).mItem.content_detail);
    }
}

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


public class ViewHolder1 extends RecyclerView.ViewHolder {
    public final View mView;
    public final ImageView mIdView;
    public final TextView mContentView;
    public final Context mContext;
    public DummyItem mItem;

    public ViewHolder1(final View view) {
        super(view);
        mView = view;
        mContext = view.getContext();
        mIdView = (ImageView) view.findViewById(R.id.id);
        mContentView = (TextView) view.findViewById(R.id.content);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                switch (position) {
                    case 0:
                        Intent i = new Intent(mContext, CropsActivity.class);
                        mContext.startActivity(i);
                        break;
                    case 1:
                        Intent i1 = new Intent(mContext, WeatherActivity.class);
                        mContext.startActivity(i1);
                }
            }
        });

    }

    @Override
    public String toString() {
        return super.toString() + " '" + mContentView.getText() + "'";
    }

}

public class ViewHolder2 extends RecyclerView.ViewHolder {
    public final View mView;
    public final ImageView mIdView;
    public final TextView mContentView, mContentDetailView;
    public final Context mContext;
    public DummyItem mItem;

    public ViewHolder2(final View view) {
        super(view);
        mView = view;
        mContext = view.getContext();
        mIdView = (ImageView) view.findViewById(R.id.crops_image);
        mContentView = (TextView) view.findViewById(R.id.crops_name);
        mContentDetailView = (TextView) view.findViewById(R.id.crops_detail);
    }

    @Override
    public String toString() {
        return super.toString() + " '" + mContentView.getText() + "'";
    }

}

}

DummyContent

package krishidarshan.dummy;

/**
* Created by Abhishek on 19-03-2017.
*/

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

import krishidarshan.R;

/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p/>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {

/**
 * An array of sample (dummy) items.
 */
public static final List<DummyItem> ITEMS = new ArrayList<>();
public static final List<DummyItem> CROPS_ITEMS = new ArrayList<>();

private static String[] item_names = {"Crops", "Weather", "Market Price", "ABC"};
private static int[] item_list = {R.drawable.bg, R.drawable.bg, R.drawable.bg, R.drawable.bg};

private static String[] crops_names = {"Rice", "Wheat", "Jwar", "Bajra"};
private static int[] crops_images = {R.drawable.bg, R.drawable.bg, R.drawable.bg, R.drawable.bg};
private static String[] crops_content = {"They are useful", "They are useful", "They are useful", "They are useful"};

private static final int COUNT = 4;

static {
    // Add some sample items.
    for (int i = 0; i < COUNT; i++) {
        addItem(createDummyItem(i));
        addItemForCrop(createCropItem(i));
    }
}

private static void addItem(DummyItem item) {
    ITEMS.add(item);
}

private static void addItemForCrop(DummyItem item) {
    CROPS_ITEMS.add(item);
}

private static DummyItem createDummyItem(int position) {
    return new DummyItem(item_list[position], item_names[position]);
}

private static DummyItem createCropItem(int position) {
    return new DummyItem(crops_images[position], crops_names[position], crops_content[position]);
}

/**
 * A dummy item representing a piece of content.
 */
public static class DummyItem {
    public final int id;
    public final String content;
    public final String content_detail;

    public DummyItem(int id, String content) {
        this.id = id;
        this.content = content;
        this.content_detail = null;
    }
    @Override
    public String toString() {
        return content;
    }

    public DummyItem(int id, String content, String content_detail) {
        this.id = id;
        this.content = content;
        this.content_detail = content_detail;
    }

 }
 }

card_view.xml

<?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:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:elevation="4dp"
android:foreground="?selectableItemBackground"
card_view:cardBackgroundColor="#ffffff"
card_view:cardCornerRadius="6dp"
card_view:contentPadding="0.2dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignBottom="@+id/id"
        android:alpha="0.8"
        android:background="#757575"
        android:gravity="center"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:text="Crops"
        android:textSize="17dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

试试这个,它应该有效:

if (holder instanceof ViewHolder1) {
    ((ViewHolder1) holder).bind(mValues.get(position));
} else {
// more code here! ...
}