我想使用Glide获取图像网址的图像,但出现错误 我不知道为什么。
我已经检查了我该怎么办,但是我不明白我的源代码中有什么问题
public class ProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
private ArrayList<Product> productList;
String intent_first;
String intent_last;
String intent_phnumber;
String intent_addr;
public ProductAdapter(ArrayList<Product> productList, Context c, String pre_first, String pre_last, String detail_addr, String phNumber){
this.mContext = c;
this.productList = productList;
this.intent_first = pre_first;
this.intent_last = pre_last;
this.intent_addr = detail_addr;
this.intent_phnumber = phNumber;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView proimage;
TextView pro_name;
TextView pro_cont;
TextView pro_price;
MyViewHolder(View view){
super(view);
proimage = view.findViewById(R.id.productimage);
pro_name = view.findViewById(R.id.productname);
pro_cont = view.findViewById(R.id.productcont);
pro_price= view.findViewById(R.id.productprice);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int position) {
final MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
final int Position = position;
Log.e("result",productList.get(position).productimage);
Glide.with(mContext).load(productList.get(position).productimage).into(myViewHolder.proimage);
myViewHolder.pro_name.setText(productList.get(position).productname);
myViewHolder.pro_cont.setText(productList.get(position).productcont);
myViewHolder.pro_price.setText(productList.get(position).productprice + "원");
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(v.getContext(),myViewHolder.text1.getText(), Toast.LENGTH_SHORT).show();
// Toast.makeText(v.getContext(),myViewHolder.etp_name.getText(), Toast.LENGTH_SHORT).show();
String proname = (String) myViewHolder.pro_name.getText();
String proprise = (String) myViewHolder.pro_price.getText();
String procont = (String) myViewHolder.pro_cont.getText();
Intent intent = new Intent(v.getContext(), ProductOrderActivity.class);
intent.putExtra("proname", proname);
intent.putExtra("proprise",proprise);
intent.putExtra("procont", procont);
intent.putExtra("first", intent_first);
intent.putExtra("last", intent_last);
intent.putExtra("ph", intent_phnumber);
intent.putExtra("addr", intent_addr);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
'productimage'保存的路径img网址
此代码发生了错误
Glide.with(mContext).load(productList.get(position).productimage).placeholder(R.drawable.hotel).into(myViewHolder.proimage);
我想知道是什么问题...
这是我的控制台错误
2019-06-10 18:23:20.901 21127-21127/com.example.blogapp D/AndroidRuntime: Shutting down VM
2019-06-10 18:23:20.904 21127-21127/com.example.blogapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blogapp, PID: 21127
java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
at com.bumptech.glide.Glide.getRetriever(Glide.java:684)
at com.bumptech.glide.Glide.with(Glide.java:716)
at com.example.together.Adapter.ProductAdapter.onBindViewHolder(ProductAdapter.java:88)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1308)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.widget.ScrollView.onMeasure(ScrollView.java:350)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
2019-06-10 18:23:20.904 21127-21127/com.example.blogapp E/AndroidRuntime: at android.view.View.measure(View.java:23169)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:1)
如所提到的错误,java.lang.NullPointerException:您无法在尚未附加的View或Fragment上启动加载,而其中getActivity()返回null的片段(通常是在Fragment附加之前调用getActivity()或碎片被摧毁后)。
您的片段视图尚未附加,请确保已初始化适配器并将其附加到片段的onViewCreated方法内的回收站视图。有了这个,您的活动将为空。在片段的onCreatedView之后调用OnViewCreated,以确保在创建视图后对片段执行所有操作。
同样,不需要将上下文传递给适配器,因为它似乎是google的不良做法。如果您需要适配器中的上下文,则可以使用view.getcontext()方法获取上下文。