public class MainActivity extends Activity { String[] object = { "刘备", "关羽", "张飞", "赵云", "马超", "诸葛亮" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.listview1); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.item, object); listView.setAdapter(arrayAdapter); } }
mainactivity布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
item布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哈哈" android:textSize="25sp" /> </LinearLayout>
一运行就会报错,报出错误内容为:
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
刚学Android求大神指教,这是怎么回事。。?
蜂之谷
萧一郎
相关分类