RecyclerView:尝试调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()”

RecyclerView: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()'

提问人:OISHIK SENGUPTA 提问时间:5/28/2022 最后编辑:CommonsWareOISHIK SENGUPTA 更新时间:5/29/2022 访问量:178

问:

首先,我对 android 开发完全是菜鸟,我正在为我的大学制作这个项目,我在 Recycler 视图中显示项目,每个项目都有一个删除按钮,它会更新产品数量,重新加载 recycer 视图。

显示回收器视图,问题在于在删除项目时刷新它。

适配器类

public class ProductAdaptar extends RecyclerView.Adapter<ProductAdaptar.ProductAdaptarVH> {

private List<UserLoginResp> productListItems;
private Context context;

public ProductAdaptar() {
}

public void setData(List<UserLoginResp> productListItems) {
    this.productListItems = productListItems;
    notifyDataSetChanged();
}

@NonNull
@Override
public ProductAdaptarVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    context = parent.getContext();
    return new ProductAdaptar.ProductAdaptarVH(LayoutInflater.
            from(context).inflate(R.layout.row_products,parent,false));
}

@Override
public void onBindViewHolder(@NonNull ProductAdaptarVH holder, int position) {
    UserLoginResp userLoginResp = productListItems.get(position);
    String pName = userLoginResp.getProductName();
    Log.e("Pname",""+pName);
    String pQuan = userLoginResp.getQuantity();
    Log.e("Pquan",""+pQuan);
    String pPrice = userLoginResp.getProductPrice();
    Log.e("Pprice",""+pPrice);
    String pBarcode = userLoginResp.getProductID();
    Log.e("PBarcode",""+pBarcode);
    String userID = userLoginResp.getUserID();
    Log.e("userid",""+userID);
    holder.pName.setText(pName);
    holder.pQuan.setText(pQuan);
    holder.pPrice.setText(pPrice);
    holder.delProdcut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            delProduct(userID,pBarcode);
        }
    });

}

public void delProduct(String userID, String pBarcode){
    Call<String> res = ApiClient.getUserPageService().deleteProduct(pBarcode,userID);
    res.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if(response.isSuccessful()){
                new UserPage().getAllProducts(userID);
            }
        }
        @Override
        public void onFailure(Call<String> call, Throwable t) {
                Log.e("deletefailed",""+t);
                new UserPage().getAllProducts(userID);
        }
    });
}

@Override
public int getItemCount() {
        return productListItems.size();
}

public static class ProductAdaptarVH extends RecyclerView.ViewHolder {

    TextView pName;
    TextView pQuan;
    TextView pPrice;
    Button delProdcut;

    public ProductAdaptarVH(@NonNull View itemView) {
        super(itemView);
         pName=itemView.findViewById(R.id.pName);
         pQuan=itemView.findViewById(R.id.pQuantity);
         pPrice=itemView.findViewById(R.id.pPrice);
         delProdcut=itemView.findViewById(R.id.delProduct);
    }
}
}

用户页面活动

public class UserPage extends AppCompatActivity implements ProductAdaptar.deleteProduct {
    Toolbar toolbar;
    RecyclerView recyclerView;
    String rfidNo;
    ProductAdaptar productAdaptar;
    Call<List<UserLoginResp>> productList;
    List<UserLoginResp> productListsItems = new ArrayList<>();
    

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_page);
        toolbar=findViewById(R.id.toolbar);
        recyclerView=findViewById(R.id.recyclerview);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        productAdaptar = new ProductAdaptar(this::delProduct);

        Intent intent =getIntent();
        if(intent.getExtras()!=null){
            rfidNo= intent.getStringExtra("rfid");
        }

        getAllProducts(rfidNo);
    }
        public void getAllProducts(String rfidno){
            productAdaptar = new ProductAdaptar();
            productList= ApiClient.getUserPageService().getCartItems(rfidno);
            productList.enqueue(new Callback<List<UserLoginResp>>() {
                @Override
                public void onResponse(Call<List<UserLoginResp>> call, Response<List<UserLoginResp>> response) {
                    if (response.isSuccessful()) {
                        productListsItems = response.body();
                        productAdaptar.setData(productListsItems);
                        RecyclerView recyclerView = findViewById(R.id.recyclerview);
                        recyclerView.setAdapter(productAdaptar);
                    }
                }
                @Override
                public void onFailure(Call<List<UserLoginResp>> call, Throwable t) {
                    Log.e("listfailed",t.getLocalizedMessage());
                }
            });
    }



@Override
public void delProduct(UserLoginResp userLoginResp) {
        Log.e("delted prodcut", userLoginResp.toString());
}
}

因此,api 运行良好,数据库正在更新,问题是 Recycler 视图无法刷新,因为我正在调用函数,而该函数又调用了 from 其未能刷新它。 显示回收器视图,问题在于在删除项目时刷新它。delProduct()getAllProducts()Userpage Activity

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.shopeasy, PID: 27509
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:675)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:633)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
        at com.example.shopeasy.UserPage$1.onResponse(UserPage.java:67)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$retrofit2-DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$$ExternalSyntheticLambda1.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:874)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        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)

我对 android 编码完全陌生,所以我似乎找不到修复它的方法。

安卓 android-recyclerview nullpointerexception android-context android-viewholder

评论

0赞 CommonsWare 5/29/2022
请编辑您的问题并发布整个堆栈跟踪,而不仅仅是错误行。
1赞 a_local_nobody 5/29/2022
context = parent.getContext();我从未见过这个,所以我不知道这是否真的有效,但只需更改您的适配器以将上下文作为参数,将其从您的片段/活动中传入,直到您了解更好的方法
0赞 OISHIK SENGUPTA 5/29/2022
@CommonsWare做到了。
0赞 OISHIK SENGUPTA 5/29/2022
@a_local_nobody如何传递上下文,因为当我尝试传递Userpage.this时,它说“UserPage不是关闭类”

答:

1赞 CommonsWare 5/29/2022 #1

切勿自行创建活动的实例。

取代:

private Context context;

public ProductAdaptar() {
}

跟:

private UserPage context;

public ProductAdaptar(UserPage activity) {
  context = activity;
}

然后,将所有出现的 替换为 ,例如:new UserPage()context

public void delProduct(String userID, String pBarcode){
    Call<String> res = ApiClient.getUserPageService().deleteProduct(pBarcode,userID);
    res.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if(response.isSuccessful()){
                context.getAllProducts(userID);
            }
        }
        @Override
        public void onFailure(Call<String> call, Throwable t) {
                Log.e("deletefailed",""+t);
                context.getAllProducts(userID);
        }
    });
}

另外,删除:

context = parent.getContext();

...并将对构造函数的调用替换为将活动作为参数的构造函数:ProductAdaptarUserPage

productAdaptar = new ProductAdaptar(this);

评论

0赞 a_local_nobody 5/29/2022
如果你想重用这个适配器,你可能不必有,你可以把它改回 exist,然后从viewholder的rootView中获取它,从你的活动中传入它只是初学者最简单的事情private UserPage context;private Context context;