我尝试创建一个ListView,其中填充了一些字符串,我从XML文件中读取了一个放在ArrayList结构中的字符串。我只想将它们显示为屏幕上的列表。 当我的代码达到此指令时:
MainList.setAdapter(itemsAdapter);
它只是崩溃,我无法理解为什么......任何想法,拜托?
<?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">
<ListView
android:id="@+id/data_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
public class HomeFragment extends Fragment {
Context ct;
public HomeFragment(Context c){
ct=c;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ArrayList<Zone> zones = new ArrayList<Zone>();
try{
MY CODE
}catch(Exception e){
}
ArrayList<String> items = new ArrayList<String>();
for (Zone zn : zones){items.add(zn.name);}
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(getActivity(), R.layout.fragment_home,items);
ListView MainList = (ListView)rootView.findViewById(R.id.data_list);
MainList.setAdapter(itemsAdapter);
return rootView;
}
}
**** **** EDIT
10-01 17:55:36.757: E/ArrayAdapter(22387): You must supply a resource ID for a TextView
10-01 17:55:36.757: D/AndroidRuntime(22387): Shutting down VM
10-01 17:55:36.757: W/dalvikvm(22387): threadid=1: thread exiting with uncaught exception (group=0x416198b0)
10-01 17:55:36.787: E/AndroidRuntime(22387): FATAL EXCEPTION: main
10-01 17:55:36.787: E/AndroidRuntime(22387): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ListView.makeAndAddView(ListView.java:1852)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ListView.fillDown(ListView.java:687)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ListView.fillFromTop(ListView.java:748)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ListView.layoutChildren(ListView.java:1667)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:801)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.View.layout(View.java:14472)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1984)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1738)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1008)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5509)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.os.Handler.handleCallback(Handler.java:730)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.os.Looper.loop(Looper.java:137)
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.app.ActivityThread.main(ActivityThread.java:5214)
10-01 17:55:36.787: E/AndroidRuntime(22387): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 17:55:36.787: E/AndroidRuntime(22387): at java.lang.reflect.Method.invoke(Method.java:525)
10-01 17:55:36.787: E/AndroidRuntime(22387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-01 17:55:36.787: E/AndroidRuntime(22387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-01 17:55:36.787: E/AndroidRuntime(22387): at dalvik.system.NativeStart.main(Native Method)
10-01 17:55:36.787: E/AndroidRuntime(22387): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
10-01 17:55:36.787: E/AndroidRuntime(22387): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
10-01 17:55:36.787: E/AndroidRuntime(22387): ... 44 more
*编辑2 *
我可以为列表视图中的许多文本视图提供不同的属性吗?
即。一个具有特定背景属性的textview,另一个带有某种特定字体的textview ...甚至在click事件上有一些不同的行为?
答案 0 :(得分:1)
您需要为ArrayAdapter指定一个id。
之前已经提出此问题,请参阅此链接:"ArrayAdapter requires the resource ID to be a TextView" xml problems
答案 1 :(得分:0)
您需要为ArrayAdapter指定一个id。
之前已经提出过这个问题,请看这个链接:&#34; "ArrayAdapter requires the resource ID to be a TextView" xml problems
你应该有类似的东西:
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
更多信息:http://developer.android.com/reference/android/widget/ArrayAdapter.html
您的ID应该是R.layout.fragment_home
中定义的TextView,因为它是您的资源
答案 2 :(得分:0)
您需要指定文本视图ID,并且您错误地将相对布局ID提供给textview,为textview提供特定ID ArrayAdapter要求资源ID为TextView