【问题标题】:FragmentActivity casting to avoid static referenceFragmentActivity 强制转换以避免静态引用
【发布时间】:2012-06-17 22:55:49
【问题描述】:

我是 Java 和 Android 的初学者,我认为这更像是一个 Java 问题,因为它体现在 Android 中。

我正在使用 Android 支持包 (android.support.v4.app) 并在我的名为 MyActivity 的基类中使用 DialogFragment 创建一个对话框,该基类扩展了 FragmentActivity。

我的问题涉及从 DialogFragment 中的按钮的 OnClickListener 方法中的 OnClick 方法调用 MyActivity 类中的函数。

它正在工作;我只是想明白为什么。

如果我尝试直接引用该函数 (MyActivity.someFunction()),我会收到“无法从 MyActivity 类型对非静态方法 someFunction() 进行静态引用”。任何人都有一个很好的方法来解释静态与非静态以及为什么这个特定的参考是静态的?我认为这是因为 DialogFragment 被声明为静态的。声明子类/方法静态与非静态的目的是什么。也就是说,如果方法属于类而不是实例化的对象,为什么重要?

另外,在这个例子中,强制转换为什么以及如何绕过静态引用?

谢谢!

public static class myDialogFragment extends DialogFragment {
    static myDialogFragment newInstance(int whichDialog) {
        myDialogFragment f = new myDialogFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.invasive_edit_dialog, container, true);

        Button btn_apply_coords = (Button)v.findViewById(R.id.btn_get_coord);
        btn_apply_coords.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                    // This does not work ("Cannot make a static reference to the non-static method someFunction() from the type MyActivity").
                MyActivity.someFunction();


                    // This does not work ("The method someFunction() is undefined for the type FragmentActivity"). Eclipse suggests casting (a few lines down).
                getActivity().someFunction();

                    // This works; casted version of code above.  What is this code doing?
                ((MyActivity) getActivity()).someFunction();

                    // this works also
                MyActivity thisActivity =  (MyActivity) getActivity();
                thisActivity.someFunction();
            }
        });

        return v;
    }
}

public void someFunction() {
    // do something
}

【问题讨论】:

    标签: java android casting static


    【解决方案1】:
     // This does not work ("Cannot make a static reference to the non-static method someFunction() from the type MyActivity").
                MyActivity.someFunction();
    

    这不起作用,因为您尝试从类 (MyActivity) 调用此方法,而不是从该类的对象(例如 (MyActivity activity))调用此方法

     // This works; casted version of code above.  What is this code doing?
                ((MyActivity) getActivity()).someFunction();
    

    这确实有效,因为正如我所料,方法 getActivity() 返回了对象,该对象被强制转换为 MyActivity,然后在该对象上调用了来自 MyActivity 的非静态方法

    总而言之 - 如果没有可以调用它们的对象,就不能调用非静态方法。

    【讨论】:

    • 谢谢,dantuch。所以在这一行“MyActivity.someFunction();”中,我试图对类做一些事情,而不是类的实例化,即实际的对象?就 ((MyActivity) getActivity()).someFunction() 中的 2 组括号而言,我理解第一顺序是 FragmentActivity(getActivity() 返回的对象)被转换为 MyActivity 类型,是的?第二组括号是否只是确保在调用“someFunction”之前进行转换?
    • @user1214401, ( (MyActivity) getActivity() )
    • 再次感谢,dantuch。我完全理解这一点;你为我清理了很多东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    相关资源
    最近更新 更多