6

How can I show this :

public class TagsDialog extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.tags_dialog, null));
        builder.setMessage("This is a message").setTitle("TAGS");
        return builder.create();
    }
}

From inside my Fragment inside a ViewPager :

    public class MyFragment extends Fragment
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

    ...

            ImageView btnTags = (ImageView)view.findViewById(R.id.btnTags);
            btnTags.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {

                    DialogFragment dlg = new TagsDialog();
                    //this line doesn't compile
                    dlg.show(getSupportFragmentManager(), "tags");

                }
            });
}
}

I have tried for ages to get this to work, but getSupportFragmentManager is never resolved... any ideas?

EDIT:

I feel this is all caused by the support FragmentManager vs the android.app.FragmentManager, however I do not know how to solve this, as I am using the ViewPager from the support library...

getSupportFragmentManager/and all related getFragManager methods like parent and child one always returns the Manager from the support lib, wheras the show method wants the core one.

Imports are:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

FragmentManager fm = getActivity().getSupportFragmentManager(); // returns from support lib
DialogFragment dlg = new TagsDialog();
dlg.show(fm, "tags"); // wants core...

If I just use core, then getSupportFragmentManager() does not exist on getActivity()...

4

2 回答 2

14

你应该仔细检查你的进口。您不能混合使用来自支持库和 android 核心 api 的 Fragment 和 FragmentManager。

如果您使用支持,请使用支持(来自 package android.support.v4.app)的所有内容。如果没有,请使用核心 api 包 ( android.app) 中的所有内容。

于 2013-12-14T21:37:45.850 回答
4

这是我在 ViewPager 中的 Fragment 中所做的。显示 DialogFragment 的代码与您在任何地方执行的代码都是相同的。

FragmentManager fm = getActivity().getSupportFragmentManager();
ConfirmDeleteDialog dialog = new ConfirmDeleteDialog();
dialog.setTargetFragment(RecipeFragment.this, REQUEST_CONFIRM_RC);
Bundle b = new Bundle();
b.putString(ConfirmDeleteDialog.EXTRA_CONFIRM_DIALOG_TYPE,
        ConfirmDeleteDialog.CONFIRM_DIALOG_TYPE.COMMIT.name());  // enum determines what to display
dialog.setArguments(b);
dialog.show(fm, "confirm delete");

编辑。

我认为您可能缺少 * getActivity()。*getSupportFragmentManager()

于 2013-12-14T21:42:21.933 回答