I'm new to Android Studio, and have found similar questions but none with replies that apply to my current instance. I've been trying to work with RecyclerViews and have been getting the following error message whenever I open an activity with it. 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)
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().
You are using ButterKnife to bind views which is good.
But it seems that you have forgotten to initialise ButterKnife for your current Activity.
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.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With