Я новичок в Android Studio и нашел похожие вопросы, но ничего не ответил на мои запросы. Я пытался работать с RecyclerViews и получал следующее сообщение об ошибке всякий раз, когда открываю с ним действие. Код не показывает ошибок.RecyclerView Null Pointer Exception
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
Это мой адаптер:
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());
}
}
}
И мой класс активности (Имейте в виду, я использую плагин нож для масла избежать стереотипного кода)
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);
}
}
Что я с видом ?
Заранее благодарим за мудрость!
Привет, Кушань, благодарю вас за ответ! Я попробовал ваше предложение и получил новую ошибку: FATAL EXCEPTION: main Процесс: PID: 19348 android.content.res.Resources $ NotFoundException: Идентификатор ресурса строки # 0x4b – SexualPotatoes
Какая ошибка? на самом деле это правильный ответ для вашего предыдущего исключения. – Kushan
вам не хватает ссылки на String, которую вы где-то использовали – Kushan