使用RecyclerView时出现Nullpointer异常

时间:2015-05-22 21:58:19

标签: android nullpointerexception android-recyclerview

我正在关注一个Android开发教程(创建一个天气应用程序),但是我遇到了一个错误,我得到的问题是我在问题的最后隐藏了完整的logcat日志。有没有人知道我为什么会收到这个错误?我检查了一切超过10次。 NODE:minSDK的版本是14,这就是我使用支持库来使用Recycler视图的原因。

这是使用回收站视图的活动的布局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"
    android:background="@drawable/bg_gradient"
    tools:context="koemdzhiev.com.stormy.ui.HourlyForecastActivity">


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

这是我的Recycler.Adapter代码:

public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {
    private Hour[] mHours;

    public HourAdapter(Hour[] hours){
        mHours = hours;
    }


    @Override
    public HourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hourly_list_item,parent,false);
        HourViewHolder viewHolder = new HourViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(HourViewHolder holder, int position) {
        holder.bindHour(mHours[position]);
    }

    @Override
    public int getItemCount() {
        return mHours.length;
    }

    public class HourViewHolder extends RecyclerView.ViewHolder{

        public TextView mTimeLabel;
        public TextView mSummaryLabel;
        public TextView mTemperatureLabel;
        public ImageView mIconImageView;

        public HourViewHolder(View itemView) {

            super(itemView);
            mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
            mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
            mTemperatureLabel= (TextView) itemView.findViewById(R.id.temperatureLabel);
            mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
        }

        public void bindHour(Hour hour){
            mTimeLabel.setText(hour.getHour());
            mSummaryLabel.setText(hour.getSummary());
            mTemperatureLabel.setText(hour.getTemperature() + "");
            mIconImageView.setImageResource(hour.getIconId());
        }
    }
}

这是活动中的代码:

public class HourlyForecastActivity extends Activity {

    private Hour[] mHours;
    //inject the RecyclerView as member variable
    @InjectView(R.id.recyclerView) RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hourly_forecast);

        ButterKnife.inject(this);

        Intent intent = getIntent();
        Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
        mHours = Arrays.copyOf(parcelables,parcelables.length,Hour[].class);

        HourAdapter adapter = new HourAdapter(mHours);
        mRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        //if dealing with fixed size data, it is recommended to do the following...
        mRecyclerView.setHasFixedSize(true);
    }
}

这是我得到的logcat日志:

05-22 22:56:00.394  32579-32579/koemdzhiev.com.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: koemdzhiev.com.stormy, PID: 32579
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
        at koemdzhiev.com.stormy.adapters.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:61)
        at koemdzhiev.com.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:33)
        at koemdzhiev.com.stormy.adapters.HourAdapter.onBindViewHolder(HourAdapter.java:16)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4805)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4106)

2 个答案:

答案 0 :(得分:3)

您的Chart1.CancelMouse := True ID为iconIma V eView,但在您的代码中,您正在调用iconIma G eView。这很奇怪,因为你应该有一个编译错误,除非你在其他.xml文件中的其他地方有一个iconImageView。

答案 1 :(得分:1)

!有一个错字,我没有看到!在IconImageView的id中,它的iconImaveView带有&#34; v&#34;。我简直不敢相信。