java.lang.UnsupportedOperationException:尝试使用ArrayList时,AdapterView不支持addView(View)

时间:2018-08-15 21:30:31

标签: java android android-fragments arraylist android-arrayadapter

我正在尝试获取“主页”选项,以返回到“类别/选项”页面的主屏幕。我正在使用ArrayList来填充布局。在添加片段之前,该应用程序运行良好。我将“片段”添加到“主页”,现在每次单击“主页”时,应用程序崩溃,并出现上述标题的错误。 android的新手。任何帮助将不胜感激。

以下是代码段:
//主屏幕的ArrayList

    final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
    pojoArrayList.add(new pojo("Hotels", R.drawable.hotel, R.drawable.gradient_splash));
    pojoArrayList.add(new pojo("Restaurants", R.drawable.restaurant, R.drawable.gradient_splash));
    pojoArrayList.add(new pojo("Places", R.drawable.places, R.drawable.gradient_splash));
    pojoArrayList.add(new pojo("Events", R.drawable.events, R.drawable.gradient_splash));
    pojoArrayList.add(new pojo("Information", R.drawable.info, R.drawable.gradient_splash));

    homeAdapter adapter = new homeAdapter(this, pojoArrayList);
    ListView listViewItems = (ListView) findViewById(R.id.nav_list);
    listViewItems.setAdapter(adapter);


我认为打电话时我在某个地方犯了一个错误,所以我尝试了不同的方法:

public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment = null;
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        fragment = new HomeFragment();
    } else if (id == R.id.nav_hotel){
        fragment = new HotelFragment();
    } else if (id == R.id.nav_restaurants) {

    } 
    if(fragment!=null){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.nav_list, new HomeFragment());
        fragmentTransaction.commit();
    }

这些是相关的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".NavigationActivity"
    tools:showIn="@layout/app_bar_navigation">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/nav_list"
        android:padding="15dp"
        android:orientation="vertical" />

</LinearLayout>

第二

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/hotel_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp">

            <ImageView
                android:id="@+id/category_image"
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:scaleType="centerCrop"
                android:src="@drawable/hotel" />
            <View
                android:id="@+id/grad"
                android:layout_height="110dp"
                android:layout_width="match_parent"
                android:background="@drawable/gradient_splash"
                android:alpha="0.4" />

            <TextView
                android:id="@+id/category_header"
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:text="Hotels"
                android:gravity="center_vertical"
                android:textColor="@color/textBlack"
                android:textSize="25sp"
                android:textAlignment="center" />
        </FrameLayout>

</LinearLayout>

在运行应用程序并单击抽屉上的“主页”选项时,出现以下错误 java.lang.UnsupportedOperationException:AdapterView不支持addView(View)

任何帮助都非常有用。

0 个答案:

没有答案