简体   繁体   English

RecyclerView空指针异常

[英]RecyclerView Null Pointer Exception

I'm new to Android Studio, and have found similar questions but none with replies that apply to my current instance. 我是Android Studio的新用户,并且发现了类似的问题但没有回复适用于我当前的实例。 I've been trying to work with RecyclerViews and have been getting the following error message whenever I open an activity with it. 我一直在尝试使用RecyclerViews,并且每当我用它打开一个活动时都会收到以下错误消息。 The code doesn't show any errors. 代码不显示任何错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lucasrizzotto.edgeweather/com.lucasrizzotto.edgeweather.ui.HourlyForecastActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference

This is my 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 mTemperatureLabel;
        public TextView mSummaryLabel;
        public ImageView mIconImageView;

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

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

    }
}

And my Activity class (Keep in mind I'm using the ButterKnife plugin to avoid boilerplate code) 和我的Activity类(请记住我正在使用ButterKnife插件来避免样板代码)

public class HourlyForecastActivity extends AppCompatActivity {

    private Hour[] mHours;

    @BindView(R.id.recyclerView) RecyclerView mRecyclerView;

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

        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);
        mRecyclerView.setHasFixedSize(true);

    }
}

What am I overlooking? 我在俯瞰什么?

Thanks in advance for the wisdom! 提前感谢您的智慧!

Try calling: 试着打电话:

ButterKnife.bind(this);

after setting the content view 设置内容视图后

Since you are not doing this the conventional way, you need to actually call bind(). 由于您没有以传统方式执行此操作,因此您需要实际调用bind()。

You are using ButterKnife to bind views which is good. 您正在使用ButterKnife绑定视图,这是很好的。

But it seems that you have forgotten to initialise ButterKnife for your current Activity. 但似乎你忘了为你当前的Activity 初始化ButterKnife

Just do this, 就这样做,

ButterKnife.bind(this);

after this line, 在这一行之后,

setContentView(R.layout.activity_hourly_forecast);

This will initialise ButterKnife for your current Activity and you will be able to reference the RecyclerView from your layout correctly. 将为您当前的Activity初始化ButterKnife,您将能够正确引用您的布局中的RecyclerView。

Hope it helps. 希望能帮助到你。

Basicly you need to initialized mRecyclerView 基本上你需要初始化mRecyclerView

For example 例如

mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

But in your case you use ButterKnife library for this. 但在你的情况下,你使用ButterKnife库。

So you need to call its bind() method after setContentView(R.layout.activity_hourly_forecast); 所以你需要在setContentView(R.layout.activity_hourly_forecast)之后调用它的bind()方法;

setContentView(R.layout.activity_hourly_forecast);
ButterKnife.bind(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM