简体   繁体   中英

Application is crashing due to ClassCastException in Android studio

public class DialogUtil extends AppCompatDialogFragment {
private EditText mName;
private DialogUtilListener listener;

private String mTitle = "Title";
private String mNegative = "Negative";
private String mPositive = "Positive";

public void setDialog(String Name, String pos, String neg){
    mTitle = Name;
    mNegative = neg;
    mPositive = pos;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(Resource.getAppContext());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.layout_dialog, null);
    mName = view.findViewById(R.id.editName_ID);

    builder.setView(view)
            .setTitle(mTitle)
            .setNegativeButton(mNegative, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Log.d("Dialog", "Cancelled..!");
                }})
            .setPositiveButton(mPositive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String Name = mName.getText().toString();;
                    listener.applyText(Name);
                    Log.d("Dialog", "File Name saved");
                }});
    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        listener = (DialogUtilListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() +
         "must implement DialogUtilListener");
    }
}

public interface DialogUtilListener{
    void applyText(String Name);
}

I have written the above code for the dialog to get the name form the user to store a file. The code is getting crashed always in onAttach method and throwing class cast exception.

Following is the code I am using in my activity class.

private void openDialog(){
     DialogUtil mDialog = new DialogUtil();
     mDialog.setDialog("Map Name", "Save", "Cancel");
     mDialog.show(getSupportFragmentManager(), "Map Dialog");
}

private DialogUtil.DialogUtilListener mDialogListener = new DialogUtil.DialogUtilListener() {
    @Override
    public void applyText(String Name) {
        Log.d("DialogText", "Name: " + Name);
    }
};

How can I get rid of this error? Is there anyway I can use the same code with only change in the line causing the error? Thanks in advance.

I am also attaching the image of the exception appeared on the log cat for your reference. Error as appeared on log cat

您的主机活动必须实现DialogUtil.DialogUtilListener

In your activity, get rid of this:

 private DialogUtil.DialogUtilListener mDialogListener = new DialogUtil.DialogUtilListener() { @Override public void applyText(String Name) { Log.d("DialogText", "Name: " + Name); } }; 

Instead, declare that your activity implements the listener:

public class MyActivity implements DialogUtil.DialogUtilListener

And then put the interface method directly in your activity:

@Override
public void applyText(String Name) {
    Log.d("DialogText", "Name: " + Name);
}

You should do instaceof check before actually doing cast, something like below.

@Override
public void onAttach(Context context) {
     super.onAttach(context);
     try {
         if (context instanceof DialogUtilListener){
             listener = (DialogUtilListener) context;
         }
     } catch (ClassCastException e) {
         throw new ClassCastException(context.toString() +
          "must implement DialogUtilListener");
     }
}

That way you will get idea on which object you are getting and take necessary action.

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