我正在尝试创建一个包含自定义行的表。 我创建了一个single_row.xml文件,其中包含2个textviews +一个ImageView。另外我在一个drawable文件夹中添加了一些.png。
代码导致错误,我无法弄清楚。有人对错误有任何想法(甚至是代码中的错误)吗?
package com.example.listviewwitchcustomimage;
import android.R.drawable;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv;
String[] titles;
String[] descriptions;
int MyImages[] = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,
R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.j};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
titles = res.getStringArray(R.array.title);
descriptions = res.getStringArray(R.array.description);
lv = (ListView) findViewById(R.id.listView);
MyCustomAdapter adapter= new MyCustomAdapter(this, titles, MyImages, descriptions);
lv.setAdapter(adapter);
}
}
//Maak eigen Adapter
class MyCustomAdapter extends ArrayAdapter<String>{
Context context;
int[] images;
String[] MyTitles;
String[] MyDescriptions;
//constructor for ArrayAdapter
MyCustomAdapter(Context c,String[] titles,int imgs[],String[] descs){
super(c,R.layout.single_row,R.id.textView1,titles);
this.context = c;
this.images= imgs;
this.MyTitles = titles;
this.MyDescriptions=descs;
}
@Override
//At runtime moet de single_row gemaakt worden. Dat gebeurt in de getView
public View getView(int position, View convertView, ViewGroup parent) {
//Als er nog geen row gemaakt is-> converView = null. Als er al een row bestaat converView!=null
View row = convertView;
if(row==null){
//Convert XML in JAVA object
LayoutInflater inflator=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//Store java object of row in Variable.
//row verwijst naar de root van single_row -> <RelativeLayout>
row =inflator.inflate(R.layout.single_row,parent,false);
}
//middels row kan je dus de child elements bereiken/gebruiken
//sla referenves op in variabelen
ImageView image =(ImageView) row.findViewById(R.id.imageView1);
TextView title= (TextView) row.findViewById(R.id.textView1);
TextView description= (TextView) row.findViewById(R.id.textView2);
//Set content
image.setImageResource(images[position]);
title.setText(MyTitles[position]);
description.setText(position);
// TODO Auto-generated method stub
return row;
}
}
logcat的:
03-26 07:50:20.834: E/Trace(10654): error opening trace file: No such file or directory (2)
03-26 07:50:20.834: D/ActivityThread(10654): setTargetHeapUtilization:0.25
03-26 07:50:20.834: D/ActivityThread(10654): setTargetHeapIdealFree:8388608
03-26 07:50:20.834: D/ActivityThread(10654): setTargetHeapConcurrentStart:2097152
03-26 07:50:21.014: D/AndroidRuntime(10654): Shutting down VM
03-26 07:50:21.014: W/dalvikvm(10654): threadid=1: thread exiting with uncaught exception (group=0x40f3c378)
03-26 07:50:21.044: E/AndroidRuntime(10654): FATAL EXCEPTION: main
03-26 07:50:21.044: E/AndroidRuntime(10654): java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
03-26 07:50:21.044: E/AndroidRuntime(10654): at com.example.listviewwitchcustomimage.MyCustomAdapter.getView(MainActivity.java:93)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.ListView.onMeasure(ListView.java:1182)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.View.measure(View.java:15367)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:607)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:420)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.View.measure(View.java:15367)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.View.measure(View.java:15367)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.LinearLayout.measureVertical(LinearLayout.java:849)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.View.measure(View.java:15367)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
03-26 07:50:21.044: E/AndroidRuntime(10654): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.View.measure(View.java:15367)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1106)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4223)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.Choreographer.doFrame(Choreographer.java:525)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.os.Handler.handleCallback(Handler.java:615)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.os.Looper.loop(Looper.java:213)
03-26 07:50:21.044: E/AndroidRuntime(10654): at android.app.ActivityThread.main(ActivityThread.java:4787)
03-26 07:50:21.044: E/AndroidRuntime(10654): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 07:50:21.044: E/AndroidRuntime(10654): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 07:50:21.044: E/AndroidRuntime(10654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-26 07:50:21.044: E/AndroidRuntime(10654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-26 07:50:21.044: E/AndroidRuntime(10654): at dalvik.system.NativeStart.main(Native Method)
这是新消息。第93行:image.setImageResource(images[position]);
答案 0 :(得分:1)
好像你在R.array.title资源中有超过9个字符串。它必须与图像数组中的数量相同