我想使用列表视图,我已经创建了自己的布局,用于在列表视图中显示数据。这是我的代码:
public class ListActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the View to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//#1 android.R.layout.list_item, android.R.id.text1, values);
// list_item是我创建的包含文本视图和大小的布局
// Assign adapter to ListView
listView.setAdapter(adapter);
}
}
但在第1行我收到错误
list_item connot be resolved or is not a field
我还创建了list_item.xml文件,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp">
</TextView>
我哪里错了?
得到了@ByteMe的答案。现在没有错误。但是我在运行时遇到以下错误。我复制了logcat内容,这就是我得到的:
E/AndroidRuntime( 384): Uncaught handler: thread main exiting due to uncaught e
xception
E/AndroidRuntime( 384): java.lang.NullPointerException
E/AndroidRuntime( 384): at android.widget.ArrayAdapter.createViewFromRes
ource(ArrayAdapter.java:353)
E/AndroidRuntime( 384): at android.widget.ArrayAdapter.getView(ArrayAdap
ter.java:323)
E/AndroidRuntime( 384): at android.widget.AbsListView.obtainView(AbsList
View.java:1274)
E/AndroidRuntime( 384): at android.widget.ListView.measureHeightOfChildr
en(ListView.java:1147)
E/AndroidRuntime( 384): at android.widget.ListView.onMeasure(ListView.ja
va:1060)
E/AndroidRuntime( 384): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 384): at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime( 384): at android.widget.LinearLayout.measureChildBefor
eLayout(LinearLayout.java:888)
E/AndroidRuntime( 384): at android.widget.LinearLayout.measureVertical(L
inearLayout.java:350)
E/AndroidRuntime( 384): at android.widget.LinearLayout.onMeasure(LinearL
ayout.java:278)
E/AndroidRuntime( 384): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 384): at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime( 384): at android.widget.FrameLayout.onMeasure(FrameLay
out.java:245)
E/AndroidRuntime( 384): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 384): at android.widget.LinearLayout.measureVertical(L
inearLayout.java:464)
E/AndroidRuntime( 384): at android.widget.LinearLayout.onMeasure(LinearL
ayout.java:278)
E/AndroidRuntime( 384): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 384): at android.view.ViewGroup.measureChildWithMargin
s(ViewGroup.java:3023)
E/AndroidRuntime( 384): at android.widget.FrameLayout.onMeasure(FrameLay
out.java:245)
E/AndroidRuntime( 384): at android.view.View.measure(View.java:7964)
E/AndroidRuntime( 384): at android.view.ViewRoot.performTraversals(ViewR
oot.java:763)
E/AndroidRuntime( 384): at android.view.ViewRoot.handleMessage(ViewRoot.
java:1633)
E/AndroidRuntime( 384): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 384): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 384): at android.app.ActivityThread.main(ActivityThrea
d.java:4363)
E/AndroidRuntime( 384): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 384): at java.lang.reflect.Method.invoke(Method.java:5
21)
E/AndroidRuntime( 384): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 384): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:618)
E/AndroidRuntime( 384): at dalvik.system.NativeStart.main(Native Method)
I/Process ( 59): Sending signal. PID: 384 SIG: 3
I/dalvikvm( 384): threadid=7: reacting to signal 3
I/dalvikvm( 384): Wrote stack trace to '/data/anr/traces.txt'
现在有什么问题?
答案 0 :(得分:3)
尝试使用
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.text1, values);
而不是
android.R.layout.list_item
似乎为我解决了这个问题
答案 1 :(得分:0)
由于没有人回答问题的第二部分(问题的第一部分由@ByteMe回答),我将自己回答这个问题(通过列表视图基础后得到答案)。 anser非常简单 只需从Adapter构造函数中删除可选参数(第三个参数)。