所以,我要做的是有一个主菜单,然后按一个按钮,它会加载一个国家列表。
现在我似乎已经设置正确,没有我能看到的错误,但应用程序在加载此活动(列表)之前崩溃主菜单很好,我添加了另一个带有空白活动的按钮加载就好了。
logCat给了我这些错误
E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
我不确定它在textView
之外的要求继承我的XML:
<ListView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f897"
android:dividerHeight="1dp"
android:listSelector="#0f0"/>
和我的Java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class countries extends AppCompatActivity {
ListView simpleList;
String countryList[] = {"India", "China", "Australia", "Portugal", "USA","England", "NewZealand", "Germany", "France","South Africa"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countries);
simpleList = (ListView)findViewById(R.id.text1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
任何帮助?
答案 0 :(得分:1)
您的数组适配器期望布局是TextView,但看起来像R.layout.activity_countries是一个活动布局?
答案 1 :(得分:1)
您应该为每个listview的行创建自定义适配器和自定义视图。请在此处查看:Custom Adapter for List View
答案 2 :(得分:1)
变化:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
为:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countryList);
您的代码不起作用,因为您使用默认的ArrayAdapter和自定义项目布局