我需要使用字符串资源中的数组填充listview。如何将阵列传递给我的适配器?当我尝试跟随时,我得到以下错误
ArrayAdapter zooAdapter = ArrayAdapter.createFromResource(this, R.array.animals, android.R.id.text1);
zooList.setAdapter(zooAdapter);
zooList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
除了找不到资源之外,我不知道该如何处理错误日志。但是我的所有资源都在那里,我没有从eclipse中得到任何错误。 无论如何,我需要做的是使用Array资源加载我的arrayAdapter,有人知道怎么做吗?
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2184)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
at android.content.res.Resources.getLayout(Resources.java:891)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2306)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1156)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1418)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:709)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
at android.view.View.measure(View.java:15293)
at android.widget.SlidingDrawer.onMeasure(SlidingDrawer.java:270)
at android.view.View.measure(View.java:15293)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:630)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1418)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:709)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
at android.view.View.measure(View.java:15293)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2425)
at android.view.View.measure(View.java:15293)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1876)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1112)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1010)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4255)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.FileNotFoundException:
at android.content.res.AssetManager.openXmlAssetNative(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:487)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2166)
... 48 more
答案 0 :(得分:4)
我在代码中看到两件错误的事情:
1 - 您没有正确获取阵列:
getResources().getStringArray(R.array.animals);
2 - 您没有将行布局传递给适配器
// Create an ArrayAdapter from the String Array.
final ArrayAdapter<CharSequence> aa =
ArrayAdapter.createFromResource
(
getActivity(), values, R.layout.list_row
);
在这种情况下是自定义行
所有在一起:
// Create an ArrayAdapter from the String Array.
final ArrayAdapter<CharSequence> aa =
ArrayAdapter.createFromResource
(
getActivity(), getResources().getStringArray(R.array.animals), R.layout.list_row
);
答案 1 :(得分:4)
你也可以这样做:
1)将您的字符串数组存储在String []变量中,现在这将是您的源数据。
2)定义ArrayAdapter,这将让你,如何表示你的源数据。
现在该怎么做?
String[] zooString=getResources().getStringArray(R.array.animals);
ArrayAdapter<String> zooAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, zooString);
zooList.setAdapter(zooAdapter);
zooList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});