gpt4 book ai didi

java - 将数据从 Activity 传递到 Fragment 会导致 "ScrollView can host only one direct child"错误

转载 作者:行者123 更新时间:2023-12-01 09:24:34 27 4
gpt4 key购买 nike

我正在使用 this 制作应用程序教程。我对代码进行了一些更改,即我想从服务器获取数据到我的主要 Activity 并将其传递到我的 fragment 。在此之前,当我打开应用程序时,它运行得很好,但现在它崩溃了。 我已经尝试了 stackoverflow 中的一堆答案( 12345 ...) 在提出问题之前,但他们都没有回答我的问题。当我直接从 fragment 获取餐馆时,它工作得很好,但从 Activity 传递数据会导致此错误。

主要 Activity (我从服务器获取数据并将其传递给 fragment ):

TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_map)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_list)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tab_my_list)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

RestaurantsFragment restaurantsFragment = new RestaurantsFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("restaurantList", (ArrayList<? extends Parcelable>) restaurantList);
restaurantsFragment.setArguments(args);

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.sliding_tabs, restaurantsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

餐厅 fragment (我确实从主要 Activity 中获取数据,调试时从服务器获取餐厅):

public class RestaurantsFragment extends Fragment {

private static final String TAG = RestaurantsFragment.class.getSimpleName();

// Restaurants json url
private ProgressDialog pDialog;
private ArrayList restaurantList = new ArrayList<>();
private ListView listView;
private CustomListAdapter adapter;

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

Bundle bundle = this.getArguments();
if (bundle != null) {
restaurantList = bundle.getParcelableArrayList ("restaurantList");
}

listView = (ListView) view.findViewById(R.id.restaurants_list);
adapter = new CustomListAdapter(getActivity(), restaurantList);
listView.setAdapter(adapter);

pDialog = new ProgressDialog(getActivity());

pDialog.setMessage("Loading...");
pDialog.show();


return view;
}

fragment_restaurants

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".sliderfragments.RestaurantsFragment">

<ListView
android:id="@+id/restaurants_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

列表行

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp">

<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:background="@drawable/default_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />

<!-- User Name -->
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/userName"
android:textStyle="bold" />

<!-- Date -->
<TextView
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/userName"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/date" />

<!-- Time -->
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/date"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="@color/time"
android:textSize="@dimen/time" />

</RelativeLayout>

内容主

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</android.support.design.widget.AppBarLayout>

寻呼机适配器:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

switch (position) {
case 0:
MapsFragment tab1 = new MapsFragment();
return tab1;
case 1:
RestaurantsFragment tab2 = new RestaurantsFragment();
return tab2;
case 2:
MyRestaurantsFragment tab3 = new MyRestaurantsFragment();
return tab3;
default:
return null;
}
}

@Override
public int getCount() {
return mNumOfTabs;
}
}

堆栈跟踪:

10-09 12:30:12.755 32374-32374/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.kemo.restaurant, PID: 32374
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.kemo.restaurant/com.test.kemo.restaurant.MainActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:216)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5347)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

您正在将 fragment 添加到滑动选项卡 View 中。你写道:

fragmentTransaction.replace(R.id.sliding_tabs, restaurantsFragment);

选项卡布局是水平 ScrollView 的子级,请参阅文档:https://developer.android.com/reference/android/support/design/widget/TabLayout.html

我不知道你的应用程序是如何工作的,但你不能在这里将你的 fragment 添加到TabLayout,因为它会为现在允许的多个子元素(TabLaout实际上是内部的水平 ScrollView )

在我提到的 fragmentTransaction.replace() 方法中,您必须指定要在其中添加 fragment 的容器。根据您的代码,您正在使用 tabLayput 作为 viewpager 的标题,而不是替换其中的 fragment 。

在您的主要 Activity 中创建一个要添加 fragment 的位置(容器)。绝对不要在您的 Viewpager 标题旁边的 R.id.sliding_tabs 中。

关于java - 将数据从 Activity 传递到 Fragment 会导致 "ScrollView can host only one direct child"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942860/

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