【问题标题】:Error when calling a method from the main Fragment inside a inner DialogFragment从内部 DialogFragment 内的主 Fragment 调用方法时出错
【发布时间】:2012-08-22 05:06:42
【问题描述】:

我想从用于返回 DialogFragment 的 Dialog 的 AlertDialog 的方法 setPositiveButton() 内部的 Fragment 调用一个方法,但不能这样做。

我在一个名为 Test 的类中拥有方法 doSomething(),该类扩展了 Fragment。在这个类内部,我有一个扩展 DialogFragment 的内部类。在方法 onCreateDialog 是问题发生的地方。看代码:

public class Test extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.activity_main, null);
    return view;
}

public static class SelectDeviceDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
        .setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ((Test) getTargetFragment()).doSomething();
            }
        }).create();
    }
}

private void doSomething() {}
}

DialogFragment 和 Fragment 来自 lib android.support.v4。

发生错误是因为无法完成对 Test 的强制转换。在那种情况下我怎么能调用方法 doSomething?

谢谢。

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    在修改我的答案时,您可以走两条路。首先是将静态类更改为最终声明,如下所示:

    public final DialogFragment selectDeviceDialogFragment = new DialogFragment(){
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    doSomething();
                                }
                            }).create();
        }    
    };
    

    如果这不合适而您确实需要静态声明,则应使用接口与其通信。因为您使用的是静态类,所以假设您正在从另一个 Fragment 调用此 DialogFragment。如果是这样的话,我会走这条路:

    public interface DoSomething{
         public void doSomething();
    }
    

    现在在您的活动中,您需要实现 DoSomething 接口:

    private class MyActivity extends Activity implements DoSomething{
    
        /** other methods **/
    
        @Override
        public void doSomething() {
            FragmentManager fragmentManager = getFragmentManager();
            Test frag = (Test)fragmentManager.findFragmentById(/*the fragment id*/);
            frag.doSomething();
        }
    }
    

    现在回到内部对话框类的测试片段中:

    public static class SelectDeviceDialogFragment extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setPositiveButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    MyActivity activity = (MyActivity)getActivity();
                                    activity.doSomething();
                                }
                            }).create();
        }
    }
    

    希望这次我已经回答了你的问题:)

    【讨论】:

    • 那不行。我修改了问题以便更好地理解。
    • 希望编辑工作!请让我知道这些解决方案是否适合您
    • 不幸的是它不起作用。 FragmentActivity 是这样的stackoverflow.com/questions/12127231/…,因此我没有任何标识我的片段的 Id。
    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多