简体   繁体   English

android.content.Context.getResources() 在空对象引用上

[英]android.content.Context.getResources() on a null object reference

I have 2 classes, an Adapter class of RecyclerView and a class of Fragment.我有 2 个类,一个 RecyclerView Adapter 类和一个 Fragment 类。 When I do setOnclick in Adapter and call a method of Fragment class the application crashes.当我在 Adapter 中执行 setOnclick 并调用 Fragment 类的方法时,应用程序崩溃。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.widget.Toast.makeText(Toast.java:313)
        at com.ltud.food.Fragment.restaurantDetail.detailTabLayout.detail_datdonFragment.onAddProduct(detail_datdonFragment.java:134)
        at com.ltud.food.Fragment.restaurantDetail.detailTabLayout.FoodAdapter$1.onClick(FoodAdapter.java:73)
        at android.view.View.performClick(View.java:7155)
        at android.view.View.performClickInternal(View.java:7124)
        at android.view.View.access$3500(View.java:808)
        at android.view.View$PerformClick.run(View.java:27370)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:359)
        at android.app.ActivityThread.main(ActivityThread.java:7418)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

Adapter class:适配器类:

    @Override
    public void onBindViewHolder(@NonNull @NotNull foodViewHolder holder, int position) {
        Food food = foodArrayList.get(position);

        holder.name.setText(food.name);
        holder.price.setText(String.valueOf(food.price));
        holder.rate.setText(String.valueOf(food.rate));
        Glide.with(holder.food.getContext()).load(food.img).into(holder.food);



        holder.add_food.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                i++;
//                Toast.makeText(context,String.valueOf(i),Toast.LENGTH_SHORT).show();
                    if (!foodArrayList.get(position).isAddedTocart()){

                        foodArrayList.get(position).setAddedTocart(true);

                        detail_datdonFragment detail = new detail_datdonFragment();
                        detail.onAddProduct();

                    }


            }
        });
    }

Fragment:分段:

    @Override
    public void onAddProduct() {
        cart_count++;
            Toast.makeText(context,cart_count,Toast.LENGTH_SHORT).show();

    }

Interface:界面:

public interface AddCallbacks {
    public void onAddProduct();
}

What should I do now.我现在应该怎么办。 Can someone help me?有人能帮我吗?

Try with the below code it will help you.试试下面的代码,它会帮助你。

class DetailsFragment extends Fragment implements RVAdapter.AddCallbacks {

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    RVAdapter rvAdapter = new RVAdapter();
    rvAdapter.setCallback(this);
}

@Override
public void onAddProduct(int pos) {
     Toast.makeText(context,""+cart_count,Toast.LENGTH_SHORT).show();
}

}


 // this is your adapter class
class RVAdapter extends RecyclerView.Adapter<RVAdapter.RVHolder> {
private AddCallbacks addCallbacks;

public void setCallback(AddCallbacks addCallbacks) {
    this.addCallbacks = addCallbacks;
}

@NonNull
@Override
public RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return null;
}

@Override
public void onBindViewHolder(@NonNull RVHolder holder, int position) {
    holder.add_food.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            addCallbacks.onAddProduct(position);
        }
    });
}

@Override
public int getItemCount() {
    return 0;
}

public interface AddCallbacks {
    public void onAddProduct(int pos);
}

class RVHolder extends RecyclerView.ViewHolder {
    public RVHolder(@NonNull View itemView) {
        super(itemView);
    }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 null object 参考上调用虚拟方法“android.content.Context.getResources()” - Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference 例外:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResources()” - Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference &#39;android.content.res.Resources android.content.Context.getResources()&#39; 在一个空对象引用上。 将字符串数组转换为语音 - 'android.content.res.Resources android.content.Context.getResources()' on a null object reference. Converting String array to speech 无法实例化活动:尝试调用虚拟方法 android.content.Context.getResources() - Unable to instantiate activity:Attempt to invoke virtual method android.content.Context.getResources() 空对象引用上的android.content.Context.getPackageName()导致应用崩溃 - app crash with android.content.Context.getPackageName()' on a null object reference android.content.Context.getPackageName() 在 null object 参考 - android.content.Context.getPackageName() on a null object reference 尝试在空对象引用上调用虚拟方法&#39;android.content.Context android.content.Context.getApplicationContext()&#39; - Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 在空对象引用上接收“ android.content.Context android.content.Context.getApplicationContext()” - Receiving 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 理解Android Context:(空对象引用) - Understanding Android Context: (null object reference) 尝试在 Android Studio 中的 null object 参考上调用虚拟方法 'android.content.Context.getApplicationInfo()' - Attempt to invoke virtual method 'android.content.Context.getApplicationInfo()' on a null object reference in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM