在@override onBindViewHolder
方法中,我收到一条错误消息:
非静态字段'mWeatherTextView'无法从a引用 静态上下文
我检查了所有可能的错误但没有找到任何错误,即使我的代码与提供的解决方案类似,但是没有显示所提供解决方案的错误。
package com.example.android.sunshine;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ForecastAdapter extends
RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
private String[] mWeatherData;
public ForecastAdapter(){}
public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder{
public final TextView mWeatherTextView;
public ForecastAdapterViewHolder(View view){
super(view);
mWeatherTextView =(TextView)view.findViewById(R.id.tv_weather_data);
}
}
@Override
public ForecastAdapterViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.forecast_list_item;
boolean shouldAttachToParentImmediately = false;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layoutIdForListItem,
parent,shouldAttachToParentImmediately);
return new ForecastAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(ForecastAdapterViewHolder holder, int position)
{
String weatherForThisDay = mWeatherData[position];
ForecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);
_**/* error message says "Non-static field 'mWeatherTextView' cannot be referenced from a static context" */**_
}
@Override
public int getItemCount() {
if(mWeatherData == null) return 0;
return mWeatherData.length;
}
}
答案 0 :(得分:0)
而不是ForecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);
使用
holder.mWeatherTextView.setText(weatherForThisDay);
答案 1 :(得分:0)
在onBindViewHolder
中,您将始终使用已作为参数传递的holder
。因此,在ForecastAdapterViewHolder
行中将holder
更改为setText
。
package com.example.android.sunshine;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ForecastAdapter extends
RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
private String[] mWeatherData;
public ForecastAdapter(){}
public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder{
public final TextView mWeatherTextView;
public ForecastAdapterViewHolder(View view){
super(view);
mWeatherTextView =(TextView)view.findViewById(R.id.tv_weather_data);
}
}
@Override
public ForecastAdapterViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.forecast_list_item;
boolean shouldAttachToParentImmediately = false;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layoutIdForListItem,
parent,shouldAttachToParentImmediately);
return new ForecastAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(ForecastAdapterViewHolder holder, int position)
{
String weatherForThisDay = mWeatherData[position];
holder.mWeatherTextView.setText(weatherForThisDay);
_**/* error message says "Non-static field 'mWeatherTextView' cannot be referenced from a static context" */**_
}
@Override
public int getItemCount() {
if(mWeatherData == null) return 0;
return mWeatherData.length;
}
}