尝试调用虚拟方法 'android.content.Context: android.content.Context.getApplicationContext() FCM

Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext() FCM

提问人:sasha 提问时间:10/3/2023 最后编辑:sasha 更新时间:10/4/2023 访问量:21

问:

我有这个公共类 FcmNotificationsSender

  public FcmNotificationsSender(String userFcmToken, String title, String body, Context mContext, Activity mActivity) {
        this.userFcmToken = userFcmToken;
        this.title = title;
        this.body = body;
        this.mContext = mContext;
        this.mActivity = mActivity;


    }
    public FcmNotificationsSender(String token) {

        userFcmToken = token;
    }
    public void SendNotifications() {


       requestQueue = Volley.newRequestQueue(MainActivity.mainActivity);
        JSONObject mainObj = new JSONObject();
        try {
            mainObj.put("to", userFcmToken);

            JSONObject notiObject = new JSONObject();
            notiObject.put("title", "TestMessage ");
            notiObject.put("body", "MessageBody");

          // notiObject.put("icon", "icon"); // enter icon that exists in drawable only



           mainObj.put("notification", notiObject);


            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, postUrl, mainObj, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    System.out.println("FCM" + response);
                  //  Toast.makeText(mContext, "response", Toast.LENGTH_SHORT).show();
                    // code run is got response

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // code run is got error

                   // Toast.makeText(mContext, "bad", Toast.LENGTH_SHORT).show();
                }
            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {


                    Map<String, String> header = new HashMap<>();
                    header.put("content-type", "application/json");
                    header.put("authorization", "key=" + fcmServerKey);
                    return header;


                }
            };

         //   requestQueue.add(request);


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

这个公共类 MainAdapter 扩展了 FirebaseRecyclerAdapter<MainModel、MainAdapter.myViewHolder>

 public MainAdapter(@NonNull FirebaseRecyclerOptions<MainModel> options, Context context) {
        super(options);
        this.context = context;
    }


 protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull MainModel model) {
   
    holder.MainNumber.setText(model.MainNumber);
    holder.Primechanie.setText(model.Primechanie);

        holder.Services.setPaintFlags(holder.Services.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        holder.Otv.setPaintFlags(holder.Otv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
        holder.Type.setPaintFlags(holder.Type.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);


    holder.btnEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        final DialogPlus dialogPlus = DialogPlus.newDialog(holder.Type.getContext())
        .setContentHolder(new ViewHolder(R.layout.updateactivity))
        .setExpanded(true, 1200)
        .create();

        //
            View viewDialogPlus = dialogPlus.getHolderView();
            EditText primechanie = viewDialogPlus.findViewById(R.id.txtPrimechanie);
            EditText gosNumber = viewDialogPlus.findViewById(R.id.txtGosNumber);
            Button btnUpdate = viewDialogPlus.findViewById(R.id.btnUpdate);

            primechanie.setText(model.getPrimechanie());
            gosNumber.setText(model.getMainNumber());

            token = model.getToken();

            dialogPlus.show();

            btnUpdate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Map <String, Object> map = new HashMap<>();
                    map.put("MainNumber", gosNumber.getText().toString());
                    map.put("Primechanie", primechanie.getText().toString());
                    MainActivity mainActivity = new MainActivity();
                    String date = mainActivity.dateToMainAdapter;

                    FirebaseDatabase.getInstance().getReference().child("Tehnics/" + date)
                            .child(getRef(position).getKey()).updateChildren(map)
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void unused) {

   FcmNotificationsSender notificationsSender = new FcmNotificationsSender(token, "test", "test", viewDialogPlus.getContext(), mainActivity);
    notificationsSender.SendNotifications();
                                }
                            });

                }
            });
        }
    });

当 onsuccess 被触发时,我想通过调用 notificationsSender.SendNotifications() 向用户发送通知;

我失败了 尝试调用虚拟方法“android.content.Context” android.content.Context.getApplicationContext()' 在空对象引用上

这个 Сontext 和 Activity 呢?

我尝试了很多解决方案,包括来自这个网站的解决方案,但我还没有找到解决方案。我不久前一直在工作室工作,我对 OOP 并不熟悉。帮助。

方法 虚拟

评论


答: 暂无答案