Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
Android 在其他xml中加载ListView适配器时出错_Android_Xml_Listview - Fatal编程技术网

Android 在其他xml中加载ListView适配器时出错

Android 在其他xml中加载ListView适配器时出错,android,xml,listview,Android,Xml,Listview,我需要在main_activity.xml中创建一个mediaplayer,并在另一个xml中显示一个歌曲列表,但是当我尝试使用适配器时,我会得到一个错误 这是一个尝试使用简单数组字符串但不起作用的示例 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

我需要在main_activity.xml中创建一个mediaplayer,并在另一个xml中显示一个歌曲列表,但是当我尝试使用适配器时,我会得到一个错误 这是一个尝试使用简单数组字符串但不起作用的示例

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

    String[] phones={"Alcatel,Motorola,G2,Iphone"};
    ListAdapter adapter=new ArrayAdapter<String>(this,R.layout.real,phones);
    ListView list=(ListView) findViewById(R.id.lista);
    list.setAdapter(adapter);
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[]phones={“阿尔卡特、摩托罗拉、G2、Iphone”};
ListAdapter=newarrayadapter(this,R.layout.real,phones);
ListView列表=(ListView)findViewById(R.id.lista);
list.setAdapter(适配器);
}
其中“Lista”是ListView,“real”是另一个xml,Lista是real

请将资源id(在xml中创建的TextView id)也传递给构造函数,如

ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects);//int textViewResourceId
e、 g

ListAdapter=newarrayadapter(这个,R.layout.real,R.id.textview,电话);//text是在xml中创建的textview
查看给定的xml和必须传递的textview id

<?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:id="@+id/textview" />

</RelativeLayout>

请将资源id(在xml中创建的TextView的id)也传递给构造函数,如

ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects);//int textViewResourceId
e、 g

ListAdapter=newarrayadapter(这个,R.layout.real,R.id.textview,电话);//text是在xml中创建的textview
查看给定的xml和必须传递的textview id

<?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:id="@+id/textview" />

</RelativeLayout>

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[]phones={“阿尔卡特、摩托罗拉、G2、Iphone”};
ListAdapter=new ArrayAdapter(这个,R.layout.real,R.id.textview,电话);
ListView列表=(ListView)findViewById(R.id.lista);
list.setAdapter(适配器);
}
试试这个(Y)

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[]phones={“阿尔卡特、摩托罗拉、G2、Iphone”};
ListAdapter=new ArrayAdapter(这个,R.layout.real,R.id.textview,电话);
ListView列表=(ListView)findViewById(R.id.lista);
list.setAdapter(适配器);
}


试试这个(Y)

你能分享你的错误日志吗编译时没有日志,错误发生在打开运行时的apppCan u post时error@FelipePeña请浏览我的youtube url,在那里你可以了解它。你可以分享你的错误日志吗?编译时没有日志,在运行时打开apppCan u post时发生错误error@FelipePeña请浏览我的youtube url,在那里你可以了解我的答案与我的答案有什么区别?没有区别,但如果你愿意修改给定问题的代码,被问者将很容易理解。这就是为什么我在他们的代码中做了更改。这个文本视图在哪里?在你的XML文件中添加文本视图,id为R.id。文本视图与我的答案有什么区别?没有区别,但如果你在给定问题的代码中做了更改,被问者将很容易理解。这就是为什么我在他们的代码中做了更改。这个文本视图在哪里?在你的XML文件中添加文本视图,id为R.id.textview对不起,先生,我理解得不太清楚,我必须在另一个XML中创建一个文本视图来连接Listview,我必须在MainActivity.java中粘贴构造函数,对不起,我很新。是的,我做了,但我可以将Listview放在哪里,因为在主xml中,我不能t@FelipePeña请浏览我的youtube url,在那里你可以了解我的想法对不起,先生,我不是很了解我必须在另一个xml中创建一个textview来创建Listview,我必须在MainActivity.java中粘贴构造函数,很抱歉我很新是的,但是我可以将Listview放在哪里,因为在主xml中,我不能t@FelipePe尼娜请浏览我的youtube网址,在那里你可以了解我的想法