Android - DialogFragment(AlertDialog)中的ListFragment:Fragment没有视图

时间:2013-04-11 15:21:34

标签: android android-fragments android-dialog android-listfragment android-dialogfragment

我正在尝试使用嵌套在其中的ListFragment创建一个DialogFragment。 我正在使用4.2.2中添加的嵌套片段 我正在使用它与支持库,所以我的api版本应该无关紧要(对于记录,我的操作系统版本也是4.2.2)。

我正在尝试添加的listfragment只显示字符串,因为我正在尝试添加它,在我这样做之后,列表将有图像等等。所以请任何传递字符串列表的想法对话对我不好,我的清单会比较复杂.. 此外,我正在尝试将此片段重用于平板电脑模式。列表始终应该以该模式显示。所以重用很重要。

我的问题是当我尝试将listfragment添加到对话框的FrameLayout时,找不到它:

  

java.lang.IllegalStateException:Fragment没有视图   android.support.v4.app.Fragment $ 1.findViewById(Fragment.java:1425)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)     在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)     在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)     在android.support.v4.app.Fragment.performStart(Fragment.java:1481)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)     在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)     在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)     在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)     在   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:429)     在android.os.Handler.handleCallback(Handler.java:725)at   android.os.Handler.dispatchMessage(Handler.java:92)at   android.os.Looper.loop(Looper.java:137)at   android.app.ActivityThread.main(ActivityThread.java:5041)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:511)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)at   dalvik.system.NativeStart.main(原生方法)


增加:

也许这些新信息很重要, DialogFragment显示在SupportMapFragment中。 当我尝试在SupportMapFragment中显示ListFragment时,它可以工作。 此外,当我试图在SupportMapFragment中显示DialogFragment时 - 它可以工作。

当我尝试在DialogFragment中显示ListFragment时,它失败了。 所以它看起来像这样: FragmentActivity - > SupportMapFragment - > DialogFragment - > ListFragment。

SupportMapFragment中显示的DialogFragment如下所示:

// DialogFragment.show() will take care of adding the fragment
// in a transaction.  We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
SherlockDialogFragment prev =(SherlockDialogFragment)getChildFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
SherlockDialogFragment newFragment = new CategoriesDialogFragment();
newFragment.show(ft, "dialog");


请帮助: - )


我的DialogFragment类是这样的:

public class CategoriesDialogFragment extends SherlockDialogFragment {

// {{ Events

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.fragment_categories_dialog_fragment, null, false);

    Dialog diag = new AlertDialog.Builder(getSherlockActivity())
            .setTitle("Hello")
            .setCancelable(false)
            .setView(dialogView)
            .setPositiveButton("ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {                          

                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                }
            )
            .create();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    SherlockDialogFragment prev = (SherlockDialogFragment)getChildFragmentManager().findFragmentByTag("fragment_list_categories");
    if (prev != null) {
        transaction.remove(prev);
    }

    SherlockListFragment listCategoriesFragment = new CategoriesListFragment();
    transaction.add(R.id.fragment_list_categories, listCategoriesFragment, "fragment_list_categories").commit();

    return diag;
}
}

我的对话框布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="sjrdflksjlksjfkldsjflkjsdflksd"/>
<FrameLayout
    android:id="@+id/fragment_list_categories"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />
</LinearLayout>

我的ListFragment类是这样的:

public class CategoriesListFragment extends SherlockListFragment {
public static final String[] TITLES = 
    {
            "Henry IV (1)",   
            "Henry V",
            "Henry VIII",       
            "Richard II",
            "Richard III",
            "Merchant of Venice",  
            "Othello",
            "King Lear"
    };

// {{ Events

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.w("ListFragment", "insideeeeeeeeeeeeeeeeeeeee");
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getSherlockActivity(),
            android.R.layout.simple_list_item_1, TITLES));

    // setHasOptionsMenu(true);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO: Kazaz - implement category pressed
    super.onListItemClick(l, v, position, id);
}

// }}
}

2 个答案:

答案 0 :(得分:2)

不确定这是否适合您,但可能尝试将该代码嵌套在onCreateView中。我有类似的问题,它对我有用。

基本上删除onCreateDialog中的内容,可能是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_categories_dialog_fragment, container, false);

    // And so on...
    getDialog().setTitle("");

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    SherlockListFragment listCategoriesFragment = new CategoriesListFragment();
    transaction.add(R.id.fragment_list_categories, listCategoriesFragment, "fragment_list_categories").commit();

    return view;
}

答案 1 :(得分:0)

尝试使用以下内容: -

getSupport(Child)FragmentManager()。beginTransaction()。detach(mFragment1).replace(R.id.main,mFragment2).attach(mFragment2).addToBackStack(null).commit();

如下面的链接

所述

Google Bug