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();
}
});