首页 > 解决方案 > 从 Activity 到 Fragment:IonWrapper 错误

问题描述

当我要将 Activity 转换为片段时遇到 IonWrapper 问题:

IonWrapper 中的 IonWrapper (Context, com.codingvisions.chat_fcm.helper.IonWrapper.WebServiceInterFace) 无法应用于 (FragmentActivity, android.support.v4.app.FragmentActivity)

我尝试在 IonWrapper 中两次传递 getActivity。

/* Original function Activity */

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_richiesta_invio_pacco);

         customAlertDialog = new CustomAlertDialog(this);
        sharedPrefsHelper = new SharedPrefsHelper(this);
        webServices = new WebServices(this, new IonWrapper(this, this));
        sqliteDbManager = new SqliteDbManager(this);


        DataPartenzaInput = (TextInputEditText)findViewById(R.id.data_partenza_pacco);
        DimensioniInput = (TextInputEditText)findViewById(R.id.dimensioni_input);

        VaiButton = (Button)findViewById(R.id.vai_button);

    }

/* Function in Fragment */ 

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_richiesta_pacco, container, false);
/* .... */

   customAlertDialog = new CustomAlertDialog(getActivity());
        sharedPrefsHelper = new SharedPrefsHelper(getActivity());
        webServices = new WebServices(this, new IonWrapper(getActivity(), getActivity()));
        sqliteDbManager = new SqliteDbManager(getActivity());

/* ....*/

/* Constructor IonWrapper 

   public IonWrapper(Context context, WebServiceInterFace webServiceInterFace) {
        this.context = context;
         this.webServiceInterFace = webServiceInterFace;

    }

*/
}

标签: androidandroid-studio

解决方案


在活动和片段中实现 com.codingvisions.chat_fcm.helper.IonWrapper.WebServiceInterFace。然后在片段中初始化时将此作为第二个参数传递。


推荐阅读