Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我在android studio中有一个主要活动,错误是:适配器需要资源id textview_Java_Android_Xml_Android Studio_Android Arrayadapter - Fatal编程技术网

Java 我在android studio中有一个主要活动,错误是:适配器需要资源id textview

Java 我在android studio中有一个主要活动,错误是:适配器需要资源id textview,java,android,xml,android-studio,android-arrayadapter,Java,Android,Xml,Android Studio,Android Arrayadapter,我正在做一个android项目,其中包括一个数组适配器和listview。然而,当初始化ArrayAddWater时,我在运行时遇到了一些错误。错误:java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView 我怀疑在初始化ArrayadPath时第32行出错。或者可能是清单文件中的packagename和/或活动声明。。。。谢谢你的帮助 package org.pctechtips.menulistview; import andr

我正在做一个android项目,其中包括一个数组适配器和listview。然而,当初始化ArrayAddWater时,我在运行时遇到了一些错误。错误:
java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView
我怀疑在初始化ArrayadPath时第32行出错。或者可能是清单文件中的packagename和/或活动声明。。。。谢谢你的帮助

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);
    }
}
package org.pctechtips.menulistview;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
ArrayList主机;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hosts==新的ArrayList();
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列表=(ListView)findViewById(R.id.list);
ArrayAdapter=新的ArrayAdapter(此,R.layout.list_项,主机);
list.setAdapter(适配器);
}
/*
*为工具栏设置菜单
*/
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu,menu);
返回true;
}
/*
*工具栏中菜单选项的操作
*/
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.change_图标){
Toast.makeText(getApplicationContext(),“更改图标”,Toast.LENGTH\u SHORT.show();
返回true;
}
if(id==R.id.set\u主机名){
Toast.makeText(getApplicationContext(),“设置主机名”,Toast.LENGTH_SHORT).show();
返回true;
}
if(id==R.id.notifications){
Toast.makeText(getApplicationContext(),“通知”,Toast.LENGTH_SHORT.show();
}
返回super.onOptionsItemSelected(项目);
}
}
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>

您可以使用customize
R.layout.list\u item
而不将
textview
id作为下一个参数,因为没有这个参数,适配器内部代码将无法找到绑定数据的textview

 // another overloaded constructor 
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                                    R.layout.list_item,R.id.YourTextViewID hosts);
 //                                                    ^^^^^^^^^^^^^^^^^
//另一个重载构造函数
ArrayAdapter=新的ArrayAdapter(此,
R.layout.list_项,R.id.YourTextViewID主机);
//                                                    ^^^^^^^^^^^^^^^^^


您确保您的TextView的id为
android:id=“@android:id/text1”
您还没有发布您的布局文件,以便我知道您是否正在使用自定义
TextView
或是否要使用安卓提供的默认
TextView
。您应该在
列表项目布局中有一个
文本视图
,并在ArrayAdapter适配器中引用它的ID

此构造函数采用您定义的自定义textview的ID

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

int-resource
值由
android.R.layout.list\u视图给出,而
int-textviewsresourceid
android.R.id.list\u项给出,用于自定义
TextView

我肯定以前遇到过这个问题,只是现在不记得了。我知道我总是用三个参数(context、int、arraylist)声明ArrayAdapter;所以我可能在别的地方做错了什么,只是现在不记得了。就像有人指出的那样。我应该发布xml布局文件。我知道对于三参数构造函数,应用程序将使用默认的TextView。正如我所回答的,如果您试图使用自定义TextView(我建议您这样做),您将需要使用我发布的第一个构造函数。请检查我的xml文件并提出建议。如果我知道要提供哪个TextView,我可以有多个。我相信它仍然有效。适配器如何知道将数据放在何处我一直使用三个参数(上下文、资源、数组)声明ArrayAddPaters。。xml文件中可能有问题我不认为,我猜您可能使用了简单的\u列表\u项\u 1或其他东西,但只需发布完整的代码,这样我就可以再次检查它,尽管您可以阅读更多关于适配器的信息。我可以在此线程中发布更多代码,还是需要另一个?是的,您可以,在自定义xml列表适配器中使用
编辑
选项l textView具有id。。。我刚更新了代码好吧,我刚开始工作。我从list_item.xml中为它提供了第一个文本视图。奇怪的是,它也适用于第二个文本视图。。。这似乎是错误的。我希望能够解析信息并分配给两个不同的textview。也许我有更多需要更多信息的文本视图。。。
ArrayAdapter<String>(this, int resource, T[] values)