0

我有以下 java 文件。我刚刚尝试使用本教程创建带有图像的列表视图。它在教程中有效,但我的不是。

主.java

public class Main extends ListActivity {
String [] proList ={"Java" , "Android", "C#", "C++"} ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MyListAdapter(this,proList));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

和 MyListAdapter.java

public class MyListAdapter extends ArrayAdapter <String> {


private Context context;
private String [] values ;

public MyListAdapter(Context context, String[] values) {
    // TODO Auto-generated constructor stub
    super(context,R.layout.activity_main, values) ;
    this.context=context;
    this.values=values; 
}

@Override
public View getView(int position, View convertView, ViewGroup parent ) {
    // TODO Auto-generated method stub

    LayoutInflater inflator=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView=inflator.inflate(R.layout.activity_main, parent, false);

    TextView textView=(TextView) rowView.findViewById(R.id.textViewLAbel);
    ImageView imageView=(ImageView) rowView.findViewById(R.id.imageView1);

    textView.setText(values[position]);

    String str = values[position];

    if (str.equalsIgnoreCase("Java")){
        imageView.setImageResource(R.drawable.java);

    }

    else if (str.equalsIgnoreCase("Android")){
        imageView.setImageResource(R.drawable.android);
    }

    else if (str.equalsIgnoreCase("C#")){
        imageView.setImageResource(R.drawable.csharp) ;
    }

    else if (str.equalsIgnoreCase("C++")){
        imageView.setImageResource(R.drawable.cplus) ;
    }

    return super.getView(position, convertView, parent);
}
}

我猜XML文件中的错误,我的XML文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >

<TextView
    android:id="@+id/textViewLAbel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textSize="30sp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:src="@drawable/ic_launcher" />
 </LinearLayout>

我已经看到了两个类似的问题,但仍然找不到我的错误。

完整的 LogCat

05-09 11:04:44.866: D/dalvikvm(279): GC_EXTERNAL_ALLOC freed 827 objects / 58856 bytes in 113ms
05-09 11:08:30.596: D/dalvikvm(316): GC_EXTERNAL_ALLOC freed 849 objects / 59664 bytes in 98ms
05-09 11:08:30.646: E/ArrayAdapter(316): You must supply a resource ID for a TextView
05-09 11:08:30.656: D/AndroidRuntime(316): Shutting down VM
05-09 11:08:30.656: W/dalvikvm(316): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-09 11:08:30.726: E/AndroidRuntime(316): FATAL EXCEPTION: main
05-09 11:08:30.726: E/AndroidRuntime(316): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-09 11:08:30.726: E/AndroidRuntime(316):  at com.example.listviewdemo.MyListAdapter.getView(MyListAdapter.java:56)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ListView.makeAndAddView(ListView.java:1727)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ListView.fillDown(ListView.java:652)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ListView.fillFromTop(ListView.java:709)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ListView.layoutChildren(ListView.java:1580)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.View.layout(View.java:7035)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.View.layout(View.java:7035)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.View.layout(View.java:7035)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.View.layout(View.java:7035)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.os.Looper.loop(Looper.java:123)
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 11:08:30.726: E/AndroidRuntime(316):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 11:08:30.726: E/AndroidRuntime(316):  at java.lang.reflect.Method.invoke(Method.java:521)
05-09 11:08:30.726: E/AndroidRuntime(316):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 11:08:30.726: E/AndroidRuntime(316):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 11:08:30.726: E/AndroidRuntime(316):  at dalvik.system.NativeStart.main(Native Method)
05-09 11:08:30.726: E/AndroidRuntime(316): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-09 11:08:30.726: E/AndroidRuntime(316):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
05-09 11:08:30.726: E/AndroidRuntime(316):  ... 27 more
05-09 11:08:40.066: D/dalvikvm(336): GC_EXTERNAL_ALLOC freed 871 objects / 60520 bytes in 77ms
05-09 11:08:40.116: E/ArrayAdapter(336): You must supply a resource ID for a TextView
05-09 11:08:40.116: D/AndroidRuntime(336): Shutting down VM
05-09 11:08:40.116: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-09 11:08:40.156: E/AndroidRuntime(336): FATAL EXCEPTION: main
05-09 11:08:40.156: E/AndroidRuntime(336): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-09 11:08:40.156: E/AndroidRuntime(336):  at com.example.listviewdemo.MyListAdapter.getView(MyListAdapter.java:56)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ListView.makeAndAddView(ListView.java:1727)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ListView.fillDown(ListView.java:652)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ListView.fillFromTop(ListView.java:709)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ListView.layoutChildren(ListView.java:1580)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.View.layout(View.java:7035)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.View.layout(View.java:7035)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.View.layout(View.java:7035)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.View.layout(View.java:7035)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.os.Looper.loop(Looper.java:123)
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 11:08:40.156: E/AndroidRuntime(336):  at java.lang.reflect.Method.invokeNative(Native Method)
05-09 11:08:40.156: E/AndroidRuntime(336):  at java.lang.reflect.Method.invoke(Method.java:521)
05-09 11:08:40.156: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 11:08:40.156: E/AndroidRuntime(336):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 11:08:40.156: E/AndroidRuntime(336):  at dalvik.system.NativeStart.main(Native Method)
05-09 11:08:40.156: E/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
05-09 11:08:40.156: E/AndroidRuntime(336):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
05-09 11:08:40.156: E/AndroidRuntime(336):  ... 27 more
05-09 11:08:42.166: I/Process(336): Sending signal. PID: 336 SIG: 9
4

2 回答 2

1

问题在于线路 -

return super.getView(position, convertView, parent);

在您的适配器中。

参数 convertView 为 Null 。
根据 getView() 的文档 - convertView

在使用之前,您应该检查此视图是否为非空且类型合适。如果无法将此视图转换为显示正确的数据,则此方法可以创建一个新视图。异构列表可以指定其视图类型的数量,以便此视图始终是正确的类型(参见 getViewTypeCount() 和 getItemViewType(int))。

由于您正在创建自己的视图,因此您可以简单地返回 -

return / super.getView(position, convertView, parent) / rowView;

希望这可以澄清。

于 2013-05-09T11:08:03.560 回答
0

代替

return super.getView(position, convertView, parent);

return rowView;
于 2013-05-09T10:59:28.940 回答