0

After I finished developing the application I tried it and whenever I tried to experiment with the button that I modified it appeared to me as the application's crash and when I tried to analyze the error using my phone after I connected it to the computer I got this error

              java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Fragment.getActivity()' on a null object reference
                  at com.fbldy.app.adapters.ItemCatgorySubListAdapter.adforest_showDilogCall(ItemCatgorySubListAdapter.java:372)
                  at com.fbldy.app.adapters.ItemCatgorySubListAdapter$CustomViewHolder$2.onClick(ItemCatgorySubListAdapter.java:189)

Specifically in this part of the class :

    void adforest_showDilogCall() {
    dialog = new Dialog(context.getActivity(), R.style.customDialog);
    dialog.setCanceledOnTouchOutside(true);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_call);

    Typeface font = Typeface.createFromAsset(context.getActivity().getAssets(), "font1.ttf");
    //noinspection ConstantConditions
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.parseColor("#00000000")));

    Log.d("info call object", jsonObjectCallNow.toString() + phoneNumber);
    final TextView textViewCallNo = dialog.findViewById(R.id.textView2);
    textViewCallNo.setTypeface(font);
    final TextView verifiedOrNotText = dialog.findViewById(R.id.verifiedOrNotText);
    verifiedOrNotText.setTypeface(font);
    try {
        if (!jsonObjectCallNow.getBoolean("is_phone_verified") && phoneNumber.contains("+")) {
            phoneNumber = phoneNumber.replace("+", "");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewCallNo.setText(phoneNumber);

    Button Send = dialog.findViewById(R.id.send_button);
    Send.setTypeface(font);
    Button Cancel = dialog.findViewById(R.id.cancel_button);
    Cancel.setTypeface(font);

    try {
        if (jsonObjectCallNow.getBoolean("phone_verification")) {
            verifiedOrNotText.setVisibility(View.VISIBLE);
            if (jsonObjectCallNow.getBoolean("is_phone_verified")) {
                Toast.makeText(context.getActivity(), "sadsadsa" + jsonObjectCallNow.getBoolean("is_phone_verified"), Toast.LENGTH_LONG).show();
                verifiedOrNotText.setText(jsonObjectCallNow.getString("is_phone_verified_text"));
                verifiedOrNotText.setBackgroundResource(R.drawable.ic_verified_green_logo);
            } else {
                verifiedOrNotText.setText(jsonObjectCallNow.getString("is_phone_verified_text"));
                verifiedOrNotText.setBackgroundResource(R.drawable.ic_oncall_red_logo);
            }

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

    Send.setBackgroundColor(Color.parseColor(settingsMain.getMainColor()));
    Cancel.setBackgroundColor(Color.parseColor(settingsMain.getMainColor()));

    try {
        Send.setText(jsonObjectCallNow.getString("btn_send"));
        Cancel.setText(jsonObjectCallNow.getString("btn_cancel"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + textViewCallNo.getText().toString()));
            if (ActivityCompat.checkSelfPermission(context.getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                        context.getActivity(),
                        new String[]{Manifest.permission.CALL_PHONE},
                        1);
                return;
            }
            context.startActivity(intent);
            dialog.dismiss();
        }
    });

    Cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
}

This type of adapter type

This error basically appears when the user has a button inside the application and this is the button code

            if (callBtn != null)

            callBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    adforest_showDilogCall();
                }
            });
MrEslam
  • 1
  • 2
  • Why `context.getActivity()`?? – AskNilesh Aug 03 '18 at 06:36
  • use getActivity only in Fragment – Ahsan Malik Aug 03 '18 at 06:40
  • What is the context? (`context.getActivity()`) Maybe, it's activity or application? You need to open a dialog from activity or fragment, not from adapter. – UgAr0FF Aug 03 '18 at 11:14
  • The original source code dialog box is inside Fragment but I want to make the same dialog box appear in another activity but this activity uses a type of adapter – MrEslam Aug 03 '18 at 20:23
  • In the adapter I have to insert a getActivity() in this way context.getActivity() – MrEslam Aug 03 '18 at 20:25
  • Also all the answers you searched for do not show the same error : java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Fragment.getActivity()' on a null object reference at com.fbldy.app.adapters.ItemCatgorySubListAdapter.adforest_showDilogCall(ItemCatgorySubListAdapter.java:372) at com.fbldy.app.adapters.ItemCatgorySubListAdapter$CustomViewHolder$2.onClick(ItemCatgorySubListAdapter.java:189) – MrEslam Aug 03 '18 at 20:26
  • So all the answers here do not work with me especially since I'm still a beginner in programming and the codes in this question are different from what I'm doing – MrEslam Aug 03 '18 at 20:27

0 Answers0