無法通過方法調用轉換將實際參數android.app.FragmentManager轉換爲FragmentTransaction


今天在做一個簡單的DialogFragment作爲底部彈出框的例子的時候發生了一個bug


發現出現問題的位置很有可能是調用這個DialogFragment的Activity部分

public void showBottomDialog(View view) {
        FragmentManager fm= getSupportFragmentManager();
        BottomDialogFragment editNameDialog = new BottomDialogFragment();
        editNameDialog.show(fm,"fragment_bottom_dialog");
    }


此時的editNameDialog.show 方法下是有紅色的線的,說明此處我的方法是有問題的。

再回來看問題本身,此處的問題 中文描述的已經非常明確了 就是類型轉換錯誤,android.support.v4.app.FragmentManager 和 android.app.FragmentManager無法互相轉換,於是我們就要找到問題出現的實際原因了,爲什麼我用getSupportFragmentManager 獲取的v4包的FragmentManager無法給editNameDialog使用呢?

於是我進入editNameDialog進行查看

package com.example.jasoncool.test;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jasoncool on 2016/6/26.
 */
public class BottomDialogFragment extends DialogFragment {

    @BindView(R.id.regards_tv_100_coins)
    TextView regardsTv100Coins;
    @BindView(R.id.regards_iv_100_coins)
    ImageView regardsIv100Coins;
    @BindView(R.id.regards_ll_first_container)
    LinearLayout regardsLlFirstContainer;
    @BindView(R.id.regards_tv_2_yuan)
    TextView regardsTv2Yuan;
    @BindView(R.id.regards_iv_2_yuan)
    ImageView regardsIv2Yuan;
    @BindView(R.id.regards_ll_second_container)
    LinearLayout regardsLlSecondContainer;
    @BindView(R.id.regards_tv_8_yuan)
    TextView regardsTv8Yuan;
    @BindView(R.id.regards_iv_8_yuan)
    ImageView regardsIv8Yuan;
    @BindView(R.id.regards_ll_third_container)
    LinearLayout regardsLlThirdContainer;
    @BindView(R.id.regards_tv_12_yuan)
    TextView regardsTv12Yuan;
    @BindView(R.id.regards_iv_12_yuan)
    ImageView regardsIv12Yuan;
    @BindView(R.id.regards_ll_forth_container)
    LinearLayout regardsLlForthContainer;
    @BindView(R.id.regards_tv_coin_count)
    TextView regardsTvCoinCount;
    @BindView(R.id.regards_tv_send)
    TextView regardsTvSend;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.fragment_bottom);
        dialog.setCanceledOnTouchOutside(true);

        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        ButterKnife.bind(this, dialog);


        return dialog;
    }


}

OK 看到我的繼承的父類了嗎?沒錯,此處繼承的是android.app.DialogFragment,而不是android.support.v4.app.DialogFragment,所已問題的根源其實是出在Fragment這裏了,將此處改爲android.support.v4.app.DialogFragment 就可以和 主Activity中方法匹配上了,也就不會報類型轉換錯誤了。

當然 這裏順便就說一下爲什麼不用android.app.Fragment相關的引用呢?

因爲這個app下的fragment支持版本和support.v4.app下fragment支持的版本是不一樣的。


android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版

android.support.v4.app.Fragment 兼容的最低版本是android:minSdkVersion="4" 即1.6版


只是這個原因,相信大家也應該知道爲什麼不用app.Fragment了吧?


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章