Android 使用数组适配器和自定义xml文件的ListView
我试图显示一个简单的列表视图,每个列表项中有一个图像、一个大文本和一个小文本。我已经编写了以下代码,但当我运行应用程序时,它会给出一个NullPointerException。对于我使用的方法,我不需要查找dviewbyd,因此我不确定我要做什么来给出这个错误。有人知道吗 这是我的MenuPage.java文件Android 使用数组适配器和自定义xml文件的ListView,android,xml,listview,Android,Xml,Listview,我试图显示一个简单的列表视图,每个列表项中有一个图像、一个大文本和一个小文本。我已经编写了以下代码,但当我运行应用程序时,它会给出一个NullPointerException。对于我使用的方法,我不需要查找dviewbyd,因此我不确定我要做什么来给出这个错误。有人知道吗 这是我的MenuPage.java文件 import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; impor
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
这是上面java文件的my menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="@drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="@+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="@+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
此外,我还为我想要使用的图像创建了一个带有可绘制内容的xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="@drawable/coffee"/>
<item
android:duration="30"
android:drawable="@drawable/steak"/>
<item
android:duration="30"
android:drawable="@drawable/icecream"/>
我的logcat给出了这个错误
任何帮助都将不胜感激!谢谢我认为菜单布局文件中的问题 android:text=@+id/values 尝试编写一些文本或指定指向字符串资源的链接,如
android:text="@string/my_button_text"
当您两次调用SetListAdState时,您将使用新的OvierWrite写入第一个适配器。看看这里的例子
这是上面java文件的my menu.xml:+new ArrayAdapterthis,R.layout.activity\u main…=学习android的基本知识我知道基本知识。那只是个错误。感谢此错误:setListAdapternew ArrayAdapterthis,R.layout.activity_main,R.id.values,values;看看这个:特别是这个:final StableArrayAdapter=new StableArrayAdapter这个,android.R.layout.simple\u list\u item\u 1,list;listview.setAdapteradapter;android.R.layout.simple_list_1,列表是您感兴趣的。另外,请查看控制台中出现的错误:尝试在空对象上调用virtual方法。。。这意味着它无法处理当前布局,或者无法将源文本设置为当前布局。此外,请看ArrayAdapter文档:很抱歉,它没有真正解释如何将它与我的关联起来。你能进一步解释一下吗?如何合并两个setListAdapters?不能同时合并这两个。您需要自行实现ArrayAdapter。你可以看看这里