我是Android Studio的新手,并且发现了类似的问题,但没有一个回复适用于我当前的实例。我一直在尝试使用 RecyclerViews ,每当我用它打开一个活动时,都会收到以下错误消息。代码没有显示任何错误。
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());
}
}
}
我的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);
}
}
我在俯瞰什么?
提前感谢您的智慧!
答案 0 :(得分:4)
尝试致电:
ButterKnife.bind(this);
设置内容视图后
由于您没有以传统方式执行此操作,因此您需要实际调用bind()。
答案 1 :(得分:3)
你正在使用ButterKnife来绑定好看的视图。
但您似乎已经忘记为您当前的活动初始化ButterKnife 。
就这样做,
ButterKnife.bind(this);
在这一行之后,
setContentView(R.layout.activity_hourly_forecast);
此将为您当前的活动初始化ButterKnife,您将能够正确引用布局中的RecyclerView。
希望它有所帮助。
答案 2 :(得分:1)
基本上你需要初始化mRecyclerView
例如
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
但在你的情况下,你使用ButterKnife库。
所以你需要在setContentView之后调用它的bind()方法(R.layout.activity_hourly_forecast);
setContentView(R.layout.activity_hourly_forecast);
ButterKnife.bind(this);