我是使用滑翔的新手。我尝试从外部存储加载图像,但我在logcat中出错:
我的循环视图适配器类使用滑动来加载图像:
List<TourModel> mItems;
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder{
public ImageView mIvMain,mIvStar1,mIvStar2,mIvStar3,mIvStar4,mIvStar5;
public TextView mTvTitle,mTvPrice;
public RelativeLayout mLayoutContainer;
public ViewHolder(View mItemView) {
super(mItemView);
mIvMain = (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvMain);
mIvStar1= (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvStar1);
mIvStar2= (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvStar2);
mIvStar3= (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvStar3);
mIvStar4= (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvStar4);
mIvStar5= (ImageView) mItemView.findViewById(R.id.listFragmentMain_IvStar5);
mTvTitle = (TextView) mItemView.findViewById(R.id.listFragmentMain_TvTitle);
mTvPrice = (TextView) mItemView.findViewById(R.id.listFragmentMain_TvPrice);
mLayoutContainer = (RelativeLayout)mItemView.findViewById(R.id.listFragmentMain_LayoutContainer);
}
}
public TourListRvAdapter(Context mContext,List<TourModel> mModel) {
this.mItems = mModel;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup mViewGroup, int i) {
View mView = LayoutInflater.from(mViewGroup.getContext()).inflate(R.layout.list_fragment_main, mViewGroup, false);
ViewHolder mViewHold = new ViewHolder(mView);
return mViewHold;
}
@Override
public void onBindViewHolder(ViewHolder mViewHolder, int position) {
final TourModel mItems=this.mItems.get(position);
mViewHolder.mTvTitle.setText(mItems.getTitle());
mViewHolder.mTvPrice.setText(mItems.getPrice() + "");
Glide.with(mContext)
.load(Utility.LoadFileImg(mItems.getImage()))
.into(mViewHolder.mIvMain);
}
并且我的片段活动调用了recycleview适配器并向其添加了变量上下文。
Public class TourListFragment extends Fragment {
private static final String TAG = TourListFragment.class.getSimpleName();
private final Context mContext = getActivity();
private int position;
private List<TourModel> mListItem;
private RecyclerView mRecycleView;
private TourListRvAdapter mTouListAdapter;
public static TourListFragment newInstance(int position) {
TourListFragment f = new TourListFragment();
Bundle b = new Bundle();
b.putInt(VariableConstants.POSITION, position);
f.setArguments(b);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(VariableConstants.POSITION);
TourDAO mDao = new TourDAO(getContext());
mListItem = mDao.getAllDataReturnModel();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e(TAG, "" + position);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRecycleView = (RecyclerView) rootView.findViewById(R.id.fragmentMain_RvTourList);
mRecycleView .setHasFixedSize(true); // in content do not change the layout size of the RecyclerView
RecyclerView.LayoutManager mLayoutManager= new LinearLayoutManager(mContext); // use a linear layout manager
mRecycleView.setLayoutManager(mLayoutManager);
mTouListAdapter = new TourListRvAdapter(mContext,mListItem);
mRecycleView.setAdapter(mTouListAdapter);
//iewCompat.setElevation(rootView, 50);
return rootView;
}
}
我不知道为什么我的变量mContext返回null,任何人都可以帮助我?请。
答案 0 :(得分:3)
使用mViewHolder.itemView.getContext()
,这是您在onCreateViewHolder
中使用的相同上下文来扩充视图,而不是在适配器中保留引用。 mContext
为null,因为您正在初始化成员
private final Context mContext = getActivity();
Fragment当时没有经历其生命周期,getActivity()返回null