因为我在Android中更新鲜,所以请帮助我。单击“图像”按钮时,将显示带有单选按钮的警报“对话框”窗口。这是我用来获取警告对话框的下面的代码。
PhotosAdapter.java:
reportAbuseImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = ((Activity) mCtx).getFragmentManager();
AlertDialogRadio alert = new AlertDialogRadio();
Bundle b = new Bundle();
b.putInt("position", position);
alert.setArguments(b);
alert.show(manager, "alert_dialog_radio");
}
});
AlertDialogRadio.java:
public class AlertDialogRadio extends DialogFragment {
AlertPositiveListener alertPositiveListener;
public interface AlertPositiveListener {
public void onPositiveClick(int position);
}
public void setListener(AlertPositiveListener alertPositiveListener){
this.alertPositiveListener=alertPositiveListener;
}
public void onAttach(android.app.Activity activity) {
super.onAttach(activity);
try{
alertPositiveListener = (AlertPositiveListener) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString() + " must implement AlertPositiveListener");
}
}
OnClickListener positiveListener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog alert = (AlertDialog)dialog;
int position = alert.getListView().getCheckedItemPosition();
alertPositiveListener.onPositiveClick(position);
}
};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
int position = bundle.getInt("position");
AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setTitle("Choose your version");
b.setSingleChoiceItems(RadioButtonList.Android.code, position, null);
b.setPositiveButton("OK",positiveListener);
b.setNegativeButton("Cancel", null);
AlertDialog d = b.create();
return d;
}
}
Android.java:
public class Android {
static String[] code = new String[]{
"Not Intrested",
"Sexual Content",
"Violent or repulsive content",
"Spam or misleading",
"Infringes my rights",
"Wrong description",
"Description is not complete"
};
}
不幸的是,当点击图像按钮时,应用程序崩溃了。
LogCatError:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.splash.indivillage.indishare, PID: 10084
java.lang.ClassCastException: com.splash.indivillage.indishare.Home.HomeActivity@e7e35e6 must implement AlertPositiveListener
at com.splash.indivillage.indishare.Utils.AlertDialogRadio.onAttach(AlertDialogRadio.java:37)
at android.app.Fragment.onAttach(Fragment.java:1454)
at android.app.DialogFragment.onAttach(DialogFragment.java:365)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:937)
at android.app.BackStackRecord.setLastIn(BackStackRecord.java:860)
at android.app.BackStackRecord.calculateFragments(BackStackRecord.java:883)
at android.app.BackStackRecord.run(BackStackRecord.java:728)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1580)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6205)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
编辑:enter image description here
请帮帮我。感谢你。
答案 0 :(得分:1)
由于错误建议您必须在使用AlertDialog的HomeActivity中实施AlertPositiveListener
HomeActivity implements AlertPositiveListener
然后你必须覆盖在家庭活动的界面中声明的方法。
@Override
public void onPositiveClick(int position){
// implement your code here
}
检查this界面的工作原理。
答案 1 :(得分:0)
您正在将活动的上下文投射到Interface
引用变量中,并且父Activity
和AlertPositiveListener
之间没有任何关系。
解决方案可以是多个。不要在您的活动中使用Casting工具AlertPositiveListener
。
HomeActivity implements AlertDialogListener{
@Override
public void onPositiveClick(int position){
// Do your stuff
}
}
并在事务之前按如下方式设置侦听器。
AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this)
Bundle b = new Bundle();
b.putInt("position",position);
alert.setArguments(b);
alert.show(manager,"alert_dialog_radio");
然后只需使用alertPositiveListener
进行无需转换的回调。
答案 2 :(得分:0)
您收到错误是因为您必须覆盖fragment.interface方法中的接口方法onPositiveClick(int position)
HomeActivity implements AlertDialogListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
AlertDialogRadio alert = new AlertDialogRadio();
alert.setListener(this)
}
@Override
public void onPositiveClick(int position){
//this method you have to override
}
}
希望它能帮到你!!