'I am building an attendance app and I face this error 'void android.app.Dialog.setOwnerActivity(android.app.Activity)' How can I overcome this?
My Dialog class shows error and I don't know what happens with my code where it generate error I check multiple times and still not get a solution.
Please Help to get rid of this....
public class MyDialog extends DialogFragment {
public static final String CLASS_ADD_DIALOG = "addClass";
public static final String STUDENT_ADD_DIALOG = "addStudent";
OnClickListener listener;
public interface OnClickListener {
void onClick(String text1, String text2);
}
public void setListener(OnClickListener listener) {
this.listener = listener;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog =null;
if(getTag().equals(CLASS_ADD_DIALOG))dialog = getAddClassDialog();
//Here android studio shows an error
if(getTag().equals(STUDENT_ADD_DIALOG))dialog = getAddStudentDialog()
return dialog;
}
private Dialog getAddStudentDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog, null);
builder.setView(view);
TextView title = view.findViewById(R.id.titleDialog);
title.setText("ADD NEW STUDENT");
EditText roll_edt = view.findViewById(R.id.roll);
EditText name_edt = view.findViewById(R.id.name);
// And also here it shows error
roll_edt.setHint("Roll");
name_edt.setHint("Name");
Button cancel = view.findViewById(R.id.cancel_btn);
Button add = view.findViewById(R.id.add_btn);
cancel.setOnClickListener(v -> dismiss());
add.setOnClickListener(v -> {
String roll = roll_edt.getText().toString();
String name = name_edt.getText().toString();
roll_edt.setText(String.valueOf(Integer.parseInt(roll)+1));
listener.onClick(roll, name);
});
return builder.create();
}
private Dialog getAddClassDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog, null);
builder.setView(view);
TextView title = view.findViewById(R.id.titleDialog);
title.setText("ADD NEW CLASS");
EditText class_edt = view.findViewById(R.id.edt01);
EditText sub_edt = view.findViewById(R.id.edt02);
class_edt.setHint("Class");
sub_edt.setHint("Sub");
Button cancel = view.findViewById(R.id.cancel_btn);
Button add = view.findViewById(R.id.add_btn);
cancel.setOnClickListener(v -> dismiss());
add.setOnClickListener(v -> {
String className = class_edt.getText().toString();
String subName = sub_edt.getText().toString();
listener.onClick(className, subName);
dismiss();
});
return builder.create();
Rather than this it shows multiple error without blue lines shows error of Fragment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
