gpt4 book ai didi

android - 使用 RecyclerView 时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 15:50:24 25 4
gpt4 key购买 nike

我正在学习一个 android 开发教程(创建一个天气应用程序),但我遇到了一个错误,我在问题的末尾插入了完整的 logcat 日志。有谁知道我为什么会收到此错误?我已经检查了所有内容超过 10 次。NODE:minSDK 的版本是 14,这就是我使用支持库来使用 Recycler View 的原因。

这是使用回收器 View 的 Activity 的布局 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());
}
}
}

这是 Activity 中的代码:

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)

最佳答案

您的 ImageView id 是 iconImaVeView 但在您的代码中您调用的是 iconImaGeView。这很奇怪,因为除非在其他 .xml 文件中的其他地方有 iconImageView,否则您应该会遇到编译错误。

关于android - 使用 RecyclerView 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30406446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com