无法在片段中显示对话碎片

时间:2014-08-18 16:31:23

标签: android android-fragments

我有一个片段,它的布局有一个按钮,我添加了一个点击监听器,onclick执行它:

    CFrag_FE_Datepicker dp = new CFrag_FE_Datepicker();
    dp.show(getFragmentManager(), "dp");

CFrag_FE_Datepicker是一个对话框

然而,片段没有弹出。我在onclick proc中设置了一个断点,它按预期中断了,它很高兴地超过了dp.show(),但没有任何反应。

我将按钮移动到一个活动,但这次它没有问题。

为什么会这样?

这是片段的来源,对不起,如果这里有许多不相关的编码:

package com.Prime.no2_andr;

import java.util.Locale;

import android.os.Bundle;
import android.os.Handler;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class CFrag_FE_Datepicker extends DialogFragment implements OnClickListener{
    ViewPager mViewPager;
    boolean IsShowCal = true;
    CFrag_dp fragDP = new CFrag_dp();
    CFrag_cal fragCal = new CFrag_cal();
    private final Handler handler = new Handler();
    private Runnable runPager;
    boolean IsLoaded = false;
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//      sectPagerAdp = new SectionsPagerAdapter(getFragmentManager());
//      mViewPager = (ViewPager) getView().findViewById(R.id.pager);
//      mViewPager.setAdapter(sectPagerAdp);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.frag_fe_datepicker, container, false);
        v.findViewById(R.id.btn_More).setOnClickListener(this);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        if (IsLoaded) return;
//        IsLoaded = true;
//        runPager = new Runnable() {
//
//            @Override
//            public void run()
////            {
//              getFragmentManager().beginTransaction().add(R.id.rl_Cont, fragDP).commit();
//            }
//        };
//        handler.post(runPager);
    }

    /**
     * @see android.support.v4.app.Fragment#onPause()
     */
    @Override
    public void onPause()
    {
        super.onPause();
//        handler.removeCallbacks(runPager);
    }

    public void onClickMore(View btn) {

    }
    public FragmentTransaction ExecAddFrag(FragmentTransaction ft){

        ft.add(R.id.rl_Cont, //fragDP
                fragCal
                );
        return ft;
    }

    public static class CFrag_dp extends CFrag_dp_cal {
        public CFrag_dp() {
            super(false);
        }
    }
    public static class CFrag_cal extends CFrag_dp_cal {
        public CFrag_cal() {
            super(true);
        }
    }
    public static abstract class CFrag_dp_cal extends Fragment {
        boolean IsCal;
        public CFrag_dp_cal(boolean in_IsCal) {
            IsCal = in_IsCal;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(
                    IsCal? R.layout.frag_fe_datepicker_cal : R.layout.frag_fe_datepicker_dp,

                    container, false);
        }

    }

    @Override
    public void onClick(View v) {
        IsShowCal = !IsShowCal;
        /*
        getFragmentManager().beginTransaction()
            .replace(R.id.rl_Cont, IsShowCal? fragCal : fragDP)
            .commitAllowingStateLoss();
            */
//      ((TextView)getView().findViewById(R.id.textView1)).setText(IsShowCal? "T":"F");

    }


}

1 个答案:

答案 0 :(得分:2)

如果您正在使用嵌套片段并且在子片段上调用getFragmentManager(),则将返回的FragmentManager是父级的getChildFragmentManager()而不是宿主活动的FragmentManager。如果是这种情况,请尝试拨打getActivity().getFragmentManager()