Logcat 错误:“java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView”在尝试使用 ListView 时

我正在使用 android studio 构建一个应用程序,我正在使用 java 在 xml 文件中创建一个列表,我希望页面也有一个较低的导航栏。 它给了我那个错误

    Process: com.example.yourhealthapp,PID: 4695
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yourhealthapp/com.example.yourhealthapp.mainPage}: android.view.InflateException: Binary XML file line #11 in com.example.yourhealthapp:layout/activity_main_page: addView(View,LayoutParams) is not supported in AdapterView
        at android.app.activityThread.performLaunchactivity(activityThread.java:3450)
        at android.app.activityThread.handleLaunchactivity(activityThread.java:3602)
        at android.app.servertransaction.LaunchactivityItem.execute(LaunchactivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.activityThread$H.handleMessage(activityThread.java:2067)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.activityThread.main(activityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: android.view.InflateException: Binary XML file line #11 in com.example.yourhealthapp:layout/activity_main_page: addView(View,LayoutParams) is not supported in AdapterView
     Caused by: java.lang.UnsupportedOperationException: addView(View,LayoutParams) is not supported in AdapterView
        at android.widget.AdapterView.addView(AdapterView.java:515)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.appcompatdelegateImpl.setContentView(appcompatdelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatactivity.setContentView(AppCompatactivity.java:195)
        at com.example.yourhealthapp.mainPage.onCreate(mainPage.java:16)
        at android.app.activity.performCreate(activity.java:8000)
        at android.app.activity.performCreate(activity.java:7984)
        at android.app.Instrumentation.callactivityOnCreate(Instrumentation.java:1312)
        at android.app.activityThread.performLaunchactivity(activityThread.java:3423)
        at android.app.activityThread.handleLaunchactivity(activityThread.java:3602)
        at android.app.servertransaction.LaunchactivityItem.execute(LaunchactivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.activityThread$H.handleMessage(activityThread.java:2067)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.activityThread.main(activityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Disconnected from the target VM,address: 'localhost:50770',transport: 'socket'

我用于此活动的 Java 代码

package com.example.yourhealthapp;

import androidx.appcompat.app.AppCompatactivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class mainPage extends AppCompatactivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        ArrayList<String> foods = new ArrayList<String>();
        foods.add("foods");
        foods.add("foods");
        foods.add("foods");
        foods.add("foods");

        ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);

        ListView listView = (ListView) findViewById(R.id.list);

        listView.setadapter(itemsAdapter);
    }
}

我的xml代码是:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".mainPage">
    <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal">

            <Button
                android:id="@+id/main_main"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:fontFamily="@font/lato_bold"
                android:text="What have you eaten today"
                android:textSize="14sp" />

            <Button
                android:id="@+id/adding_main"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:fontFamily="@font/lato_bold"
                android:text="Food Adding"
                android:textSize="14sp" />

            <Button
                android:id="@+id/profile_main"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:fontFamily="@font/lato_bold"
                android:text="Profile"
                android:textSize="14sp" />

        </LinearLayout>

    </RelativeLayout>
</ListView>

那么我应该如何修复我的代码以获得一个包含项目和子项目的数组列表以及一个较低的导航栏?

wuya790 回答:Logcat 错误:“java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView”在尝试使用 ListView 时

尝试使用一个主要的相对布局并在其中添加列表视图和底部导航 喜欢

<RelativeLayout with matchparent> 
  <Listview with matchparent with layout above navigation view or your design according />
  <RelativeLayout with navigation bar size and alignparentbottom true. />
</RelativeLayout>

<LinearLayout with matchparent and orientation vertical> 
  <Listview with height 0dp and layout weight 1 />
  <RelativeLayout with any fixed height or wrapcontent />
</RelativeLayout>

希望能帮到你

本文链接:https://www.f2er.com/938.html

大家都在问