这是我的活动课,由一本书(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我可以看到一个我无法理解/调试的异常
08-04 13:43:34.382: E/AndroidRuntime(888):
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
我做错了什么?
答案 0 :(得分:3)
实际上,您使用视图ID而不是布局ID初始化arrayAdapter。这就是您收到此错误的原因。请使用此
ArrayAdapter<String> adp=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,someColors);