使用数组适配器和 listView 时出现异常
Exception when using array adapter and listView
这是我的 activity class,被一本书(Apress,"Pro Android 5")复制了,顺便说一下,这是一本充满代码错别字的书...
package com.example.list1;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView1;
private ArrayAdapter<String> listAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an object of type ArrayList from an array of strings
String[] someColors = new String[] { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "White"};
ArrayList<String> colorArrayList = new ArrayList<String>();
colorArrayList.addAll( Arrays.asList(someColors) );
// Use values from colorArraylist as values for each text1 'sbuView' used by the listView
listAdapter1 = new ArrayAdapter<String>(this, android.R.id.text1, colorArrayList);
// Tell to the listView to take data and layout from our adapter
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter( listAdapter1 );
}
}
这是我的布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.list1.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
当我保存时,Eclipse 给我没问题。当我 运行 应用程序崩溃并从 logcat 我可以看到一个我无法 understand/debug
的异常
08-04 13:43:34.382: E/AndroidRuntime(888):
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
我做错了什么?
实际上,您使用视图 ID 而不是布局来初始化 arrayAdapter id.That 这就是您获得此 error.use 此
的原因
ArrayAdapter<String> adp=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,someColors);
这是我的 activity class,被一本书(Apress,"Pro Android 5")复制了,顺便说一下,这是一本充满代码错别字的书...
package com.example.list1;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView1;
private ArrayAdapter<String> listAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an object of type ArrayList from an array of strings
String[] someColors = new String[] { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "White"};
ArrayList<String> colorArrayList = new ArrayList<String>();
colorArrayList.addAll( Arrays.asList(someColors) );
// Use values from colorArraylist as values for each text1 'sbuView' used by the listView
listAdapter1 = new ArrayAdapter<String>(this, android.R.id.text1, colorArrayList);
// Tell to the listView to take data and layout from our adapter
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter( listAdapter1 );
}
}
这是我的布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.list1.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
当我保存时,Eclipse 给我没问题。当我 运行 应用程序崩溃并从 logcat 我可以看到一个我无法 understand/debug
的异常08-04 13:43:34.382: E/AndroidRuntime(888):
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
我做错了什么?
实际上,您使用视图 ID 而不是布局来初始化 arrayAdapter id.That 这就是您获得此 error.use 此
的原因 ArrayAdapter<String> adp=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,someColors);