【问题标题】:I have this main activity in android studio with error: adapter requires resource id textview [duplicate]我在 android studio 中有这个主要活动,但出现错误:适配器需要资源 id textview [重复]
【发布时间】:2017-08-09 19:03:36
【问题描述】:

我正在开发一个涉及数组适配器和列表视图的 android 项目。但是,在初始化 arrayadpater 时,我在运行时遇到了一些错误。错误:java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 我怀疑初始化 arrayadpater 时第 32 行有错误。或者可能是清单文件中的包名和/或活动声明....任何帮助表示赞赏

package org.pctechtips.menulistview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList hosts;

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

        hosts = = new ArrayList<String>();
        hosts.add("192.168.10.100 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.101 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.102 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.103 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.104 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.105 | 33:00:11:aa:cc:bb");
        hosts.add("192.168.10.106 | 33:00:11:aa:cc:bb");

        ListView list = (ListView) findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, hosts);
        list.setAdapter(adapter);

    }

    /*
    * Infating the menu for toolbar
    */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    /*
    * actions for menu options in toolbar
    */
    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.change_icon) {
            Toast.makeText(getApplicationContext(), "Change Icon", Toast.LENGTH_SHORT).show();
            return true;
        }

        if (id == R.id.set_hostname) {
            Toast.makeText(getApplicationContext(), "Set Hostname", Toast.LENGTH_SHORT).show();
            return true;
        }

        if (id == R.id.notifications) {
            Toast.makeText(getApplicationContext(), "Notifications", Toast.LENGTH_SHORT).show();
        }

       return super.onOptionsItemSelected(item);
    }
}

list_menu.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/text_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/host_icon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.20"
            android:padding="7dp"
            android:src="@drawable/computer_48_dp"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.60"
            android:orientation="vertical"
            android:paddingLeft="0dp"
            >

            <TextView
                android:id="@+id/ip_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                tools:text="192.168.10.100"
                />

            <TextView
                android:id="@+id/mac_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16dp"
                tools:text="aa:bb:cc:00:11:22"
                />
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

main_activity.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"
    tools:context=".MainActivity" >

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="vertical">

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 好的,我刚刚开始工作了。我从我的 list_item.xml 中给了它第一个 textView。奇怪的是它也适用于第二个文本视图......这似乎是错误的。我希望能够解析信息并分配给两个不同的文本视图。也许我有更多需要更多信息的文本视图...

标签: java android xml android-studio android-arrayadapter


【解决方案1】:

您可以使用自定义 R.layout.list_item 而无需提及 textview id 作为下一个参数,因为没有这个适配器内部代码将无法找到 textview 来绑定数据

 // another overloaded constructor 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                                    R.layout.list_item,R.id.YourTextViewID hosts);
 //                                                    ^^^^^^^^^^^^^^^^^

您确保您的 TextView 的 id 为 android:id="@android:id/text1"

【讨论】:

  • 我总是用三个参数(上下文、资源、数组)声明 arrayadpaters.. xml 文件中可能有问题
  • 我不认为,我猜您可能正在使用 simple_list_item_1 或其他东西,但只需发布完整的代码,以便我可以仔细检查它,尽管您可以阅读有关适配器的更多信息constructors here
  • 我可以在这个线程中发布更多代码还是需要另一个?
  • 是的,你可以,使用edit选项
  • 我的自定义 xml 列表适配器中的所有 textView 都有一个 id...我刚刚更新了代码
【解决方案2】:

你还没有发布你的布局文件让我知道你是使用自定义TextView,还是你想使用Android提供的默认TextView。您应该在 list_item_layout 中有一个 TextView 并在 ArrayAdapter 适配器中引用它的 ID。

此构造函数获取您定义的自定义文本视图的 ID

ArrayAdapter<String>(Context context, int resource, int textViewResourceId, T[] objects)

此构造函数用于 ListView 使用带有白色背景的默认 TextView

ArrayAdapter<String>(this, int resource, T[] values)

int resource 值由android.R.layout.list_view 给出,int textViewResourceIDandroid.R.id.list_item 给出,用于自定义TextView

【讨论】:

  • 我确定我以前遇到过这种情况,只是现在不记得了。而且我知道我总是用三个参数(context、int、arraylist)声明arrayadapter;所以可能我在其他地方做错了什么,只是现在不记得了。就像有人指出的那样。我应该发布 xml 布局文件
  • 要知道对于三参数构造函数,App会使用默认的TextView。正如我所回答的,如果您尝试使用自定义 TextView(我建议您这样做),则需要使用我发布的第一个构造函数。
  • 你们可以检查我的 xml 文件并建议谢谢
  • 我怎么知道要提供哪个文本视图,我可以有多个。我它仍然有效。适配器如何知道将数据放在哪里
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多