Android AdapterView 不支持 addView(View)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22848664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:29:54  来源:igfitidea点击:

addView(View) is not supported in AdapterView

android

提问by Maurício Giordano

I'm starting in android development and I'm getting that error on the title.

我开始从事 android 开发,但在标题上遇到了该错误。

Here is my Contacts.java

这是我的 Contacts.java

package us.inevent.toot;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Contacts extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new ContactListFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contacts, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class ContactListFragment extends Fragment {

        private ViewGroup listContacts;

        public ContactListFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
        {

            View rootView = inflater.inflate(R.layout.fragment_contacts,
                    container, false);

            listContacts = (ViewGroup) rootView.findViewById(R.id.listContacts);

            TextView aux = new TextView(getActivity());

            aux.setText("Hello World!");

            listContacts.addView(aux);

            return rootView;
        }
    }

}

Inside the onCreateView()method of my ContactListFragment, I'm creating a TextViewwith "Hello World" as text.

onCreateView()my的方法中ContactListFragment,我创建了一个TextView“Hello World”作为文本。

Then, I'm trying to add that view to my ViewGrouplistContacts.

然后,我试图将该视图添加到我的ViewGrouplistContacts 中。

Here is my fragment_contacts.xml

这是我的 fragment_contacts.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="us.inevent.toot.Contacts$PlaceholderFragment" >

    <TextView
        android:id="@+id/textContact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:text="@string/textContact"
        android:textSize="@dimen/abc_action_bar_title_text_size" />

    <ListView
        android:id="@+id/listContacts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textContact"
        android:layout_marginTop="10dp" />

</RelativeLayout>

What am I doing wrong?

我究竟做错了什么?

Thanks.

谢谢。

采纳答案by codeMagic

You are trying to set it to your ListView. That's not going to work. Try setting it to it's parent. With something like

您正试图将其设置为您的ListView. 那是行不通的。尝试将其设置为它的父级。像这样的东西

listContacts.getParent().addView(aux);

Edit after comments

评论后编辑

To add an item to your ListViewyou need to add it to whatever list you use to populate your ListViewand call setAdapter()on it or notifyDataSetChanged()on your Adapter.

要将项目添加到您的项目中,您ListView需要将其添加到您用来填充ListView和调用setAdapter()它的任何列表中或notifyDataSetChanged()在您的Adapter.

You don't add items to your ListView. You add items to your Adapterand set the Adapteron the ListView.

您不会将项目添加到您的ListView. 您将项目添加到您的AdapterAdapterListView.

I suggest going through this tutorial

我建议通过本教程

and reading through the docs thoroughly.

并通读文档。

ListView

列表显示

Adapter

适配器

From the docs

从文档

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

Adapter 对象充当 AdapterView 和该视图的基础数据之间的桥梁。适配器提供对数据项的访问。Adapter 还负责为数据集中的每个项目制作一个 View。

Your ListViewis the AdapterViewhere (it could be a Spinneror other such things)

ListViewAdapterView这里(它可能是一个Spinner或其他这样的东西)

回答by Sahil Jain

Use Linear Layout instead of list View.

使用线性布局而不是列表视图。

回答by Parteekj

Put your List View in Linear Layout and then add any view in your Linear List its not gonna Give the error

将您的列表视图放在线性布局中,然后在您的线性列表中添加任何视图,它不会给出错误

<?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="wrap_content"
android:layout_height="match_parent"
android:id="@+id/llist"
tools:context="com.android.parteek.contentprovider.Main2Activity">

  <android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/serch" />


< ListView   
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android. parteek.contentprovider.Main2Activity">

</ListView>