Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 类异常转换接口_Android_Android Fragments_Interface - Fatal编程技术网

Android 类异常转换接口

Android 类异常转换接口,android,android-fragments,interface,Android,Android Fragments,Interface,我有一个片段,它在MainActivity中使用,实际上它在MainActivity的ViewPager中使用 public class Myfragment extends Fragment implements MySingleton.ResponseInterface{ public static Myfragment newInstance() { final Myfragment mf = new Myfragment(); return m

我有一个片段,它在MainActivity中使用,实际上它在MainActivity的ViewPager中使用

public class Myfragment extends Fragment implements MySingleton.ResponseInterface{


    public static Myfragment newInstance() {
        final Myfragment mf = new Myfragment();
        return mf;
    }

    public Myfragment() {
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //.....
        final MySingleton mysingleton = MySingleton.getInstance(getContext());

        //I have a button in the fragment that I use like this

        myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mysingleton.getSomeResponse();
        }
    });

}

@Override
    public void onResponseGiven(String response) {
        Log.d("response", response);
    }

}
我有一个用于不同操作的Singleton类,Singleton包括一个接口:

public class MySingleton{


    private static MySingleton mInstance;
    private static Context mContext;
    public ResponseInterface responseInterface;

    private MySingleton(Context context){
        mContext = context;
        this.responseInterface = (ResponseInterface) context;

    }

    public static synchronized MySingleton getInstance(Context context){
        if(mInstance == null){
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public void getSomeResponse(){
       responseInterface.onResponseGiven("send response");
    }


    public interface ResponseInterface{
        void onResponseGiven(String response);
    }
}
为什么我会得到ClassCastException,告诉我不能将MainActivity强制转换为MySingleton.ResponseInterface???

如果您将MainActivity强制转换为MySingleton.ResponseInterface,您必须确保它正在实现

在实际代码中,MyFragment实现了MySingleton.ResponseInterface。 我想没有

当您调用MySingleton.getResponse时,您正在将消息发送到singleton中的responseInterface。我猜您的活动,因为它是创建singleton实例时的上下文

public void getSomeResponse(){
   responseInterface.onResponseGiven("send response");
}
如果您想得到片段的答案,您必须确保singleton正在调用您的片段实例

public void getSomeResponse(){
   responseInterface.onResponseGiven("send response");
}
无论如何,有一个非常严重的设计问题。如果在销毁片段/活动实例时不释放它们,则将其附加到singleton会导致memeory泄漏。阅读

此外,您的singleton并不是真正的singleton,您每次调用getIntance后都会创建一个新实例。

您应该将其传递给getInstance方法:

 MySingleton.getInstance(this);
并尝试将其转换为MySingleton.ResponseInterface而不是MySingleton。

在您的MySingleton类中

在你的碎片里


您将获得类强制转换异常,因为在最后一行MySingleton MySingleton=MySingleton.getInstancegetContext;,您正在传递活动上下文,并试图将该上下文转换为ResponseInterface。ResponseInterface由Myfragment实现

要解决这个问题,您可以如下更改MySingeton类构造函数

MyFragment OnCreateView中的调用部分为

在getInstance方法中


当然不是,但我想得到MyFragment的响应,如果我让MainActivity实现Mysingleton.ResponseInterfce响应转到MainActivity,对吗?实现接口,意味着您有一个兼容的接口实例。它不会通过所有实现在您的单例中神奇地调用它。阅读我的编辑我认为,在我的singleton实现中,如果singleton实例已经存在,则不会创建新实例。你确定吗?…没有NullPointerException?你可以检查我的答案。@eddiemysingleton.getSomeResponsethis;正在OnClickListener内部调用,因此此操作无效。您可以重试@埃迪:我认为你的答案是唯一一个符合预期的答案。非常感谢。这给了SingletonI的getInstance方法一个错误。我认为解决方案可能有效,只是,我总是在同一个片段上得到响应,我想得到名为getSomeResponse method的片段的响应,描述一下你想用这个逻辑实现什么,我想能够从任何片段调用单例方法getSomeResponse,并将响应返回到进行调用的片段中。
final MySingleton mysingleton = MySingleton.getInstance(getActivity());
// edited here
mysingleton.getSomeResponse(Myfragment.this);
private MySingleton(Context context, ResponseInterface responseInterface){
    mContext = context;
    this.responseInterface = responseInterface;

}
final MySingleton mysingleton = MySingleton.getInstance(getContext(), this);
 public static synchronized MySingleton getInstance(Context context, ResponseInterface responseInterface){
    if(mInstance == null){
        mInstance = new MySingleton(context, responseInterface);
    }
    return mInstance;
}