我正在编写一个使用自定义列表的活动。在列表的每一行中,我想要一个图像视图、一个文本视图,最后是一个图像按钮。下面是布局的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_marginTop="9dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
现在java文件中的代码:
public class Contacts extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1, getResources().getStringArray(R.array.main_menu)));
}
private class MyAdapter extends ArrayAdapter<String> // Adapter Class to create a custom list
{
// Constructor
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings)
{
// Calls super constructor
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.contact_layout, parent); // Represents a single row
String[] contacts = getResources().getStringArray(R.array.main_menu);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1); // Reference to image view
TextView tv = (TextView) row.findViewById(R.id.textView1); // Reference to text view
ImageButton ib = (ImageButton) row.findViewById(R.id.imageButton1); // Reference to image button
// Manipulating the stuff on a row
tv.setText(contacts[position]);
iv.setImageResource(R.drawable.ic_launcher);
ib.setImageResource(R.drawable.ic_launcher);
return row;
}
}
}
代码没有错误。它在我正在关注的视频讲座中运行良好。但我总是得到UnsupportedOperationException。请帮忙
更新:错误跟踪
09-17 08:35:00.898: E/Trace(9405): 错误打开跟踪文件: 没有这样的文件或目录 (2) 09-17 08:35:41.187: E/AndroidRuntime(9405): 致命异常: main 09 -17 08:35:41.187: E/AndroidRuntime(9405): java.lang.UnsupportedOperationException: addView(View, LayoutParams) 在 AdapterView 中不支持 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android .widget.AdapterView.addView(AdapterView.java:477) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:497) 09-17 08:35 :41.187: E/AndroidRuntime(9405): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.LayoutInflater.inflate( LayoutInflater.java:352) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 com.rakeshsarangi.petrofiesta2013.Contacts$MyAdapter。getView(Contacts.java:45) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.AbsListView.obtainView(AbsListView.java:2299) 09-17 08:35:41.187: E/ AndroidRuntime(9405): 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.ListView.onMeasure(ListView.java:1156 ) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android .widget.RelativeLayout.measureChild(RelativeLayout.java:602) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415) 09-17 08:35 :41.187: E/AndroidRuntime(9405): 在 android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.ViewGroup。measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-17 08:35:41.187: E/ AndroidRuntime(9405): 在 android.view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:833 ) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:574) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android .view.View.measure(View.java:15172) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816) 09-17 08:35 :41.187: E/AndroidRuntime(9405): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 com。android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2435) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.View.measure(View.java:15172 ) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android .view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275) 09-17 08:35 :41.187: E/AndroidRuntime(9405): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.ViewRootImpl$TraversalRunnable。运行(ViewRootImpl.java:4214)09-17 08:35:41.187:E/AndroidRuntime(9405):在 android.view.Choreographer$CallbackRecord。运行(Choreographer.java:725)09-17 08:35:41.187:E/AndroidRuntime(9405):在 android.view.Choreographer.doCallbacks(Choreographer.java:555)09-17 08:35:41.187:E/ AndroidRuntime(9405): 在 android.view.Choreographer.doFrame(Choreographer.java:525) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java :711) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.os.Handler.handleCallback(Handler.java:615) 09-17 08:35:41.187: E/AndroidRuntime(9405):在 android.os.Handler.dispatchMessage(Handler.java:92) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 android.os.Looper.loop(Looper.java:137) 09-17 08 :35:41.187:E / AndroidRuntime(9405):在android.app.ActivityThread.main(ActivityThread.java:4931)09-17 08:35:41.187:E / AndroidRuntime(9405):在java.lang.reflect。Method.invokeNative(Native Method) 09-17 08:35:41.187: E/AndroidRuntime(9405): at java.lang.reflect.Method.invoke(Method.java:511) 09-17 08:35:41.187: E /AndroidRuntime(9405): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 09-17 08:35:41.187: E/AndroidRuntime(9405): 在 com.android.internal。 os.ZygoteInit.main(ZygoteInit.java:558) 09-17 08:35:41.187: E/AndroidRuntime(9405): at dalvik.system.NativeStart.main(Native Method)35:41.187:E/AndroidRuntime(9405):在 dalvik.system.NativeStart.main(本机方法)35:41.187:E/AndroidRuntime(9405):在 dalvik.system.NativeStart.main(本机方法)