简体   繁体   中英

Why dosen't the alert dialog show?

I have a custom dialogue box, it is supposed to get a value from the user but, it is not showing. Can anyone help?

This is my code.

Custom Dialog Code

package com.aadi.pointer.Dialogs;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;

import com.aadi.pointer.R;
import com.google.android.material.switchmaterial.SwitchMaterial;

import java.util.Objects;

public class EnterParentPinDialog extends AppCompatDialogFragment 
{
   public interface PosBtnCallback {
       void setListener(DialogInterface dialog, int which, Context mContext,EditText et_parent_pin, boolean doNotAskMeAgain);
   }

   public interface NegBtnCallback {
       void setListener(DialogInterface dialog, int which);
   }

   private PosBtnCallback posBtnCallback;
   private NegBtnCallback negBtnCallback;
   private Context mContext;
   private String title = null;

   public EnterParentPinDialog( PosBtnCallback posBtnCallback, NegBtnCallback negBtnCallback, Context mContext) {
       this.posBtnCallback = posBtnCallback;
       this.negBtnCallback = negBtnCallback;
       this.mContext = mContext;
   }

   public EnterParentPinDialog( PosBtnCallback posBtnCallback, NegBtnCallback negBtnCallback, Context mContext,String title) {
       this.posBtnCallback = posBtnCallback;
       this.negBtnCallback = negBtnCallback;
       this.mContext = mContext;
       this.title = title;
   }

   @NonNull
   @Override
   public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
       AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()));
       
       LayoutInflater inflater = getActivity().getLayoutInflater();
       View view = inflater.inflate(R.layout.layout_dialog, null);

       EditText et_parent_pin = view.findViewById(R.id.et_parent_pin);
       SwitchMaterial switch_dnama = view.findViewById(R.id.switch_dnama);
       TextView dnama = view.findViewById(R.id.dnama);
       
       if(title == null) {
           switch_dnama.setVisibility(View.VISIBLE);
           dnama.setVisibility(View.VISIBLE);
           /*switch_dnama.setOnCheckedChangeListener((buttonView, isChecked) -> {
               if(isChecked) builder.setView(view)
                           .setTitle("Enter Your Parent Pin")
                           .setNegativeButton("cancel", (dialog, which) ->negBtnCallback.setListener(dialog,which))
                           .setPositiveButton("Accept", (dialogInterface, i) -> posBtnCallback.setListener(dialogInterface,i,mContext,et_parent_pin,true));

               else builder.setView(view)
                           .setTitle("Enter Your Parent Pin")
                           .setNegativeButton("cancel", (dialog, which) ->negBtnCallback.setListener(dialog,which))
                           .setPositiveButton("Accept", (dialogInterface, i) -> posBtnCallback.setListener(dialogInterface,i,mContext,et_parent_pin,false));

           });*/
       } else if(title != null){
           switch_dnama.setVisibility(View.INVISIBLE);
           dnama.setVisibility(View.INVISIBLE);
           builder.setView(view)
                   .setTitle(title)
                   .setNegativeButton("cancel", (dialog, which) ->negBtnCallback.setListener(dialog,which))
                   .setPositiveButton("Accept", (dialogInterface, i) -> posBtnCallback.setListener(dialogInterface,i,mContext,et_parent_pin, false));

       }
       return builder.create();
   }
}

Activity where the custom dialog was called

package com.aadi.pointer.Kids;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aadi.pointer.Dialogs.EnterParentPinDialog;
import com.aadi.pointer.Dialogs.EnterReasonandValueDialogClass;
import com.aadi.pointer.ListAdapters.ActionListAdapter;
import com.aadi.pointer.Nav.HomeActivity;
import com.aadi.pointer.R;
import com.aadi.pointer.Storage.CloudFirestoreControl;
import com.aadi.pointer.Storage.MyApplication;
import com.aadi.pointer.Storage.StorageStructure.Action;
import com.aadi.pointer.Storage.StorageStructure.ActionList;
import com.aadi.pointer.Storage.StorageStructure.Learner;
import com.aadi.pointer.Storage.StorageStructure.Parent;
import com.aadi.pointer.Utils;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.ArrayList;
import java.util.List;


@SuppressLint("ALL")
public class KidHomeActivity extends AppCompatActivity implements ActionListAdapter.OnRecyclerListItemClick, View.OnClickListener, PopupMenu.OnMenuItemClickListener {
    private static final String TAG = "KidHomeActivity";
    List<Integer> selectedActionPositions;
    FirebaseFirestore db;
    TextView tv_KidName,tv_name,tv_kidpoints;
    ImageView btn_back,iv_kid_avatar,iv_kid_rank,btn_add;
    RelativeLayout btn_more_options;
    RecyclerView rv_actions;
    CloudFirestoreControl control;
    MyApplication myApplication;
    Learner clickedLearner;
    Parent currentParent;
    ActionList actionList;
    int pointsint, beginningPoints;
    ActionListAdapter actionListAdapter;
    Context context;
    Intent i;
    boolean editable;
    String phoneNo;

    @SuppressLint("ALL")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kid_home);
        setUp();
        setUpRVActionList();
    }
    private void setUp(){
        TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        phoneNo = manager.getLine1Number();
        db = FirebaseFirestore.getInstance();
        tv_KidName = findViewById(R.id.tv_KidName);
        tv_name  = findViewById(R.id.tv_name);
        tv_kidpoints = findViewById(R.id.tv_kidpoints);
        btn_back = findViewById(R.id.btn_back);
        iv_kid_avatar = findViewById(R.id.iv_kid_avatar);
        iv_kid_rank = findViewById(R.id.iv_kid_rank);
        btn_add = findViewById(R.id.btn_add);
        rv_actions = findViewById(R.id.rv_actions);
        btn_more_options = findViewById(R.id.btn_more_options);
        selectedActionPositions = new ArrayList<>();
        btn_more_options.setOnClickListener(v -> {
            PopupMenu popup = new PopupMenu(this, v);
            popup.setOnMenuItemClickListener(this);
            popup.inflate(R.menu.menu_kid_home_more_options);
            popup.show();
        });

        context = this;

        i  = new Intent(this,EditKidDataActivity.class);

        control = new CloudFirestoreControl();
        myApplication = (MyApplication) getApplicationContext();
        clickedLearner = myApplication.getClickedLearner();

        btn_add.setOnClickListener(v -> new EnterReasonandValueDialogClass((dialog, which, mContext, et_reason, et_value) -> {
            control.setAction(phoneNo,clickedLearner.getScreen_Name(),et_reason.getText().toString(),Integer.parseInt(et_value.getText().toString()));
            actionList.getActionList().add(new Action(et_reason.getText().toString(),Integer.parseInt(et_value.getText().toString())));
            actionListAdapter.notifyDataSetChanged();
        },(dialog, which) -> dialog.dismiss(),v.getContext()).show(getSupportFragmentManager(),"Enter Reason and value"));

        myApplication.setSignedInPhoneNo("+919945922138");
        //myApplication.setSignedInPhoneNo(Utils.getPhoneNoWithTelephonyManager(this,this));

        control.getParentData(phoneNo,(fullname, screenname, age, phoneNo1, pin, askParentPin) -> {

            currentParent = new Parent(fullname, screenname,phoneNo,age,null,null,pin,askParentPin);
            if(currentParent.getPin() == 0){
                new AlertDialog.Builder(this)
                        .setTitle("Oops, you do not have a parent pin")
                        .setMessage("Oops, we think you forgot to set your parent pin" +
                                "press ok to enter your parent pin")
                        .setPositiveButton("Ok",(dialog, which) -> {
                            EnterParentPinDialog pinDialog = new EnterParentPinDialog((dialog1, which1, mContext, et_parent_pin,dontAskMeAgain) -> currentParent.setPin(Integer.parseInt(et_parent_pin.getText().toString())),(dialog1, which1) -> dialog1.dismiss() ,this,"Enter Your Parent Pin");
                            pinDialog.show(getSupportFragmentManager(), "Get points");
                            Utils.delay(2,() ->
                                    new AlertDialog.Builder(this)
                                            .setTitle("Heads Up!")
                                            .setMessage("In case you forgot or want to change your parent pin," +
                                                    "you can do that my going to the account page (Click the account" +
                                                    " icon in the home screen). There you can press change parent pin and enter a new one")
                                            .setPositiveButton("Ok",(dialogInterface, i) -> dialogInterface.dismiss()).show());
                        }).show();
            }
        });
        control.getActionList(phoneNo,myApplication.getClickedLearner().getScreen_Name(),actionList1 -> actionList = actionList1);
        pointsint = clickedLearner.getHomePoints();
        tv_KidName.setText(clickedLearner.getScreen_Name());
        tv_name.setText(clickedLearner.getScreen_Name());
        tv_kidpoints.setText(Integer.toString(clickedLearner.getHomePoints()));
        btn_back.setOnClickListener(this);
        beginningPoints = clickedLearner.getHomePoints();
        clickedLearner.worthyOfsetiv(iv_kid_rank);
    }
    private void setUpRVActionList(){
        rv_actions.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));
        control.getActionList(phoneNo,clickedLearner.getScreen_Name(),actionList1 -> {
                rv_actions.setAdapter(new ActionListAdapter(actionList1,this));
                actionListAdapter = new ActionListAdapter(actionList1,this);
                actionList = actionList1;
        });
    }

    @Override
    public void onClick(View v) {
        for (Action a: actionList.getActionList()) {
            control.setAction(phoneNo,clickedLearner.getScreen_Name(),a.getReason(),a.getPoints());
        }
        if(pointsint - beginningPoints == 0){
            control.saveParentData(phoneNo,
                    currentParent.getFull_Name(),
                    currentParent.getScreen_Name(),
                    currentParent.getAge(),
                    currentParent.getPin());
            Utils.toActivity(this, HomeActivity.class);
        }
        else {
            AlertDialog saveCheck =  new AlertDialog.Builder(v.getContext())
                    .setTitle("Change in Points")
                    .setMessage("Your kids points had changed, do you want to save it?")
                    .setPositiveButton("Yes", null)
                    .setNegativeButton("No", null)
                    .show();
            saveCheck.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v1 -> {
                clickedLearner.worthyOfsetiv(iv_kid_rank);
                myApplication.setClickedLearner(clickedLearner);
                control.saveLearnerData(phoneNo,
                        clickedLearner.getScreen_Name(),
                        clickedLearner.getFull_Name(),
                        clickedLearner.getAge(),
                        clickedLearner.getRank().toString(),
                        null,
                        null,
                        clickedLearner.getParentsID(),
                        null,
                        false,
                        0,
                        pointsint);
                control.saveParentData(phoneNo,
                        currentParent.getFull_Name(),
                        currentParent.getScreen_Name(),
                        currentParent.getAge(),
                        currentParent.getPin());
                Utils.toActivity(this,HomeActivity.class);
            });
            saveCheck.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(v1 -> {
                control.saveParentData(phoneNo,
                        currentParent.getFull_Name(),
                        currentParent.getScreen_Name(),
                        currentParent.getAge(),
                        currentParent.getPin());
                clickedLearner.setHomePoints(beginningPoints);
                Utils.toActivity(this,HomeActivity.class);
            });

        }
    }

    @Override
    public void onClick(int position) {
        if (editable){
            Log.i(TAG, "onClick: editing");
            new AlertDialog.Builder(this)
                    .setTitle("Edit Points")
                    .setMessage("You are editing your kids actions" +
                            "if you want to edit an action click edit. " +
                            "If you want to delete an action, press delete " +
                            "Click on the action you want to edit or delete")
                    .setPositiveButton("Edit",
                            (dialog, which) -> new EnterReasonandValueDialogClass((dialog1, which1, mContext, et_reason, et_value) -> {
                                control.deleteDocument(db.collection("Parents")
                                        .document(phoneNo)
                                        .collection("Learners")
                                        .document(clickedLearner.getScreen_Name())
                                        .collection("Actions")
                                        .document(actionList.getActionList().get(position).getReason()));
                                control.setAction(phoneNo,clickedLearner.getScreen_Name(),et_reason.getText().toString(),Integer.parseInt(et_value.getText().toString()));
                                setUpRVActionList();
                                editable = false;
                            },
                            (dialog1, which1) -> {
                                dialog1.dismiss();
                                editable = false;
                            },this).show(getSupportFragmentManager(), "Get points"))
                    .setNegativeButton("Delete", (dialog, which) -> {
                        control.deleteDocument(db.collection("Parents")
                                .document(phoneNo)
                                .collection("Learners")
                                .document(clickedLearner.getScreen_Name())
                                .collection("Actions")
                                .document(actionList.getActionList().get(position).getReason()));
                        setUpRVActionList();
                        editable = false;
                    }).show();
            actionListAdapter.notifyDataSetChanged();
        }
        else {
            Log.d(TAG, "onClick:" + currentParent.isAskParentPin());
            if(currentParent.isAskParentPin()){
                addPoints(actionList.getActionList().get(position).getPoints());
            } else if(!currentParent.isAskParentPin()) {
                new EnterParentPinDialog((dialog, which, mContext, et_parent_pin, doNotAskMeAgain) -> {
                    control.saveParentData(
                            currentParent.getPhoneNumber(),
                            currentParent.getFull_Name(),
                            currentParent.getScreen_Name(),
                            currentParent.getAge(),
                            currentParent.getPin(),
                            Boolean.toString(doNotAskMeAgain));
                    currentParent.setAskParentPin(doNotAskMeAgain);
                    addPoints(actionList.getActionList().get(position).getPoints());
                },(dialog, which) -> dialog.cancel(),this).show(getSupportFragmentManager(), null);
            }
        }
    }



    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.edit_kid_item:
                Log.i(TAG, "onMenuItemClick: Edit Kid Data");
                context.startActivity(new Intent(context, EditKidDataActivity.class));
                return true;
            case R.id.edit_action_item:
                editable = true;
                Log.i(TAG, "onMenuItemClick: Edit Action");
                new AlertDialog.Builder(this)
                        .setTitle("Edit kid Actions")
                        .setMessage("Click on the action you want to edit")
                        .setPositiveButton("Ok", (dialog, which) -> dialog.dismiss())
                        .show();
                return true;
            case R.id.delete_item:
                // do your code
                Log.i(TAG, "onMenuItemClick: Delete Kid");
                new AlertDialog.Builder(this)
                        .setTitle("Are you sure you want to delete this kid?")
                        .setMessage("You wont be able to undo this")
                        .setPositiveButton("Sure",(dialog, which) -> {
                            control.deleteKid(
                                    db.collection("Parents")
                                            .document(phoneNo)
                                            .collection("Learners")
                                            .document(clickedLearner.getScreen_Name()));
                            Utils.toActivity(this,HomeActivity.class);
                        })
                        .setNegativeButton("No",(dialog, which) -> dialog.dismiss()).show();

                return true;
            default:
                return false;
        }
    }
    private void addPoints(int value){
        if(value == 0) Log.d(TAG, "onClick: Didnt Get points");
        else {
            pointsint += value;
            tv_kidpoints.setText(Integer.toString(pointsint));
            clickedLearner.setHomePoints(pointsint);
            clickedLearner.worthyOfsetiv(iv_kid_rank);
        }
    }

}

When I try to open the dialog, a greyish screen appears but, i do not get my layout. The dialog is to open when an item of a recycler view is clicked I get this error message :


2021-01-09 10:13:59.581 12265-12308/com.aadi.pointer I/om.aadi.pointe: JIT allocated 74KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
2021-01-09 10:14:20.973 12265-12356/com.aadi.pointer W/Firestore: (22.0.1) [WatchStream]: (4d8522f) Stream closed with status: Status{code=CANCELLED, description=Disconnecting idle stream. Timed out waiting for new targets., cause=null}.
2021-01-09 10:16:45.210 12265-12265/com.aadi.pointer W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed. **When i click out to stop the grey screen**

I am new to android so I don't know what to do. I have not found anything like this.

Thanks in advance

The problem with the "Edit Points" dialog is that you are not calling create before showing it. Also, you should add curly braces to your positive button action.

You should edit the code as:

new AlertDialog.Builder(this)
                    .setTitle("Edit Points")
                    .setMessage("You are editing your kids actions" +
                            "if you want to edit an action click edit. " +
                            "If you want to delete an action, press delete " +
                            "Click on the action you want to edit or delete")
                    .setPositiveButton("Edit", (dialog, which) -> {
                        new EnterReasonandValueDialogClass((dialog1, which1, mContext, et_reason, et_value) -> {
                            ...
                        }
                    })
                    .setNegativeButton("Delete", (dialog, which) -> {
                        ...
                    }).create().show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM