由于Android Studio中的ClassCastException,应用程序崩溃了

时间:2018-11-01 15:11:35

标签: java android

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

我已经为对话框编写了上面的代码,以获得用户存储文件的名称。代码总是在onAttach方法中崩溃,并抛出类强制转换异常。

以下是我在活动课程中使用的代码。

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

如何摆脱这个错误?无论如何,我只能在导致错误的行中更改使用相同的代码? 预先感谢。

我还要附上出现在日志猫上的例外图片,以供您参考。Error as appeared on log cat

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

在您的活动中,摆脱这一点:

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

相反,声明您的活动实现了侦听器:

public class MyActivity implements DialogUtil.DialogUtilListener

然后将接口方法直接放在您的活动中:

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

答案 2 :(得分:-1)

您应该在实际进行投射之前进行instaceof检查,如下所示。

@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");
     }
}

这样,您将了解要获取的对象并采取必要的措施。