यह DialogFragment कार्यान्वयन का कारण बनता है एकIllegalStateException
IllegalStateException ("आप संवाद के OnCancelListener या OnDismissListener सेट नहीं कर सकता")
("आप संवाद के OnCancelListener या OnDismissListener सेट नहीं कर सकता")। क्यूं कर? उपाय?
public class OkCThreadDialog1 extends DialogFragment{
DialogInterface.OnCancelListener onCancelListener;
public OkCThreadDialog1(){
}
public static OkCThreadDialog1 newInstance(String title, String message) {
OkCThreadDialog1 frag = new OkCThreadDialog1();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
frag.setArguments(args);
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder .setTitle(getArguments().getString("title"))
.setMessage(getArguments().getString("message"))
.setOnCancelListener(onCancelListener)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
getDialog().cancel();
}});
return builder.create();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
onCancelListener = (DialogInterface.OnCancelListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement OkCancelDialogListener");
}
}
}
मेरी गतिविधि DialogInterface.OnCancelListener
पालन लागू करता है:
public class MainActivity extends Activity implements OkCancelDialogListener{
static final String TAG ="MainActivity";
@Override
public void onCancel(DialogInterface dialog) {
}
}
Exeception builder.create();
से फेंक दिया है। क्या गलत है?
इसके लिए धन्यवाद। यह स्वीकार्य उत्तर होना चाहिए। –