Android - getActivity()从dialogFragment返回null

时间:2014-07-12 10:25:28

标签: android android-viewpager android-dialogfragment

我对碎片很新,所以如果这是非常基本的话我会道歉。

我的应用包含一个ViewPager,可以使用ListView在几个不同的标签之间滑动。在单击一个特定的ListView项目时,我想打开一个显示一些选项的DialogFragment。我的目的是让DialogFragment处理click事件并将选定的对话框项反馈给调用它的片段。我这样做是通过向MainActivity(包含ViewPager)提供回调并将该信息提供给正确的ViewPager片段。

我注意到我的DialogFragment在getActivity()上返回null,我认为这是因为我的DialogFragment没有正确附加到我的MainActivity。我怎样才能做到这一点?考虑到我是回调新手,我发现有点难以浏览这个问题,大多数其他处理片段的例子都是标准片段,而不是来自ViewPager的片段。

我的对话框如何识别正确的活动?我应该将片段附加到FragmentManager吗?如果是这样,它是否应该与ViewPager片段连接到同一个FragmentManager?

这是我的代码,但我不确定它会有多大帮助。

CalibrationFragment.java - 调用DialogFragment。 (不确定这是否是正确的方法。)

ModeDialogFragment modeDialog = ModeDialogFragment.newInstance(R.string.mode_calibration);
modeDialog.onCreateDialog(null);

ModeDialogFragment.java - 我的DialogFragment

public class ModeDialogFragment extends DialogFragment{

    public static ModeDialogFragment newInstance(int title) {
        ModeDialogFragment frag = new ModeDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);

        return frag;
    }

    public void onAttach(Activity activity){
        super.onAttach(activity);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
        final Context mContext = getActivity();
        getActivity().getFragmentManager().beginTransaction().add(newInstance(R.string.mode_calibration), "my_fragment").commit();
        // Trying to add my dialog to the MainActivity, not working.

        final CharSequence[] modeItems = {
                "Option 1", "Option 2", "Option 3"
        };
        final int checkedItem = 2; // Make final for now, resolve this later

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(getResources().getString(R.string.mode_calibration));
            builder.setSingleChoiceItems(modeItems, checkedItem, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                // Handle the selection here

                ((MainActivity)getActivity()).updateModeData();
            }
        });
        return(builder.create());
    }
}

MainActivity.java

    public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
        // Initialise variables
        private Context mContext;
        // [Others...]

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

            // Initialisation
            viewPager = (ViewPager) findViewById(R.id.pager);
            actionBar = getActionBar();
            mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    }

   public Fragment findFragmentByPosition(int position) {
        int viewId = R.id.pager;
        //FragmentPagerAdapter fragmentPagerAdapter = getFragmentPagerAdapter();
        return getSupportFragmentManager().findFragmentByTag(
                makeFragmentName(viewId, position));
    }

    // Needed to identify a fragment from ViewPager
    private static String makeFragmentName(int viewId, int position)
    {
         return "android:switcher:" + viewId + ":" + position;
    }

    public void updateModeData(){
        // Make call to the fragment to deal with the data
        CalibrationFragment calFragment = (CalibrationFragment) findFragmentByPosition(3);
        ((CalibrationFragment) calFragment).doUpdateModeData();
        }
}

任何帮助或指导都将不胜感激!

编辑:问题确实是我没有将片段添加到FragmentManager并正确调用它。这是我使用的代码。 我的代码仍未完整贯穿,但出于此问题的目的,我的DialogFragment现在可以访问MainActivity。

CalibrationFragment.java - 调用DialogFragment

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

ModeDialogFragment modeDialog = ModeDialogFragment.newInstance(R.string.mode_calibration);
String title = makeFragmentName(R.id.pager, 3); // makeFragmentName as specified in MainActivity.java
modeDialog.show(ft, title);

ModeDialogFragment.java与之前相同,但删除了以下行:

getActivity().getFragmentManager().beginTransaction().add(newInstance(R.string.mode_calibration), title).commit();

1 个答案:

答案 0 :(得分:1)

你不应该自己打电话给onCreateDialog。只需使用show调用FragmentTransaction方法:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ModeDialogFragment modeDialog = ModeDialogFragment.newInstance(R.string.mode_calibration);
modeDialog.show(ft, null);

此处描述了DialogFragment的正确用法:http://developer.android.com/reference/android/app/DialogFragment.html