gpt4 book ai didi

Android - DialogFragment(AlertDialog) 中的 ListFragment : Fragment does not have a view

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:17:21 25 4
gpt4 key购买 nike

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

我尝试添加的列表 fragment 现在只显示字符串,因为我正在尝试添加它,在我添加它之后,列表将包含图像等。所以请传递字符串列表的任何想法对话框对我不好,我的列表会更复杂..此外,我正在尝试将此 fragment 重新用于平板电脑模式。该列表应始终以该模式显示。所以重用很重要。

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

java.lang.IllegalStateException: Fragment does not have a view at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) at android.support.v4.app.Fragment.performStart(Fragment.java:1481) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) at 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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)


添加:

也许这个新信息很重要,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);
}

// }}
}

最佳答案

不确定这是否适合您,但也许可以尝试将该代码嵌套在 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;
}

关于Android - DialogFragment(AlertDialog) 中的 ListFragment : Fragment does not have a view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15952665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com