我创建了一个 CursorAdapter 但我不断收到以下错误:
06-01 20:36:58.890: E/AndroidRuntime(21204): java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup
这是我的代码:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
final LayoutInflater inflater = LayoutInflater.from(context);
//***The error originates from this line***
final View view = inflater.inflate(R.layout.competitor_autocomplete_list_item, parent, false);
TextView stockNameTextView = (TextView) view.findViewById(R.id.autocomplete_stock_name);
TextView stockSymbolTextView = (TextView) view.findViewById(R.id.autocomplete_stock_symbol);
String stockName = cursor.getString(cursor.getColumnIndex(StournamentConstants.TblStocks.COLUMN_NAME));
String stockSymbol = cursor.getString(cursor.getColumnIndex(StournamentConstants.TblStocks.COLUMN_EXTERNAL_ID));
stockNameTextView.setText(stockName);
stockSymbolTextView.setText(stockSymbol);
return view;
}
我的布局:
<?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="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/autocomplete_stock_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#95B9C7"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:id="@+id/autocomplete_stock_symbol"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#95B9C7"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" />
</LinearLayout>
我基本上想使用带有两个 TextViews 的 LinearLayout,但又使用带有两个 TextViews 的 View,因为我认为这会解决这个错误。我尝试转换为其他类型,但我被卡住了 - 我不明白我在这里做错了什么。
谁能看到我错过了什么?谢谢!
更新
我将布局更改为使用LinearLayout
. 现在我收到以下错误:06-02 00:48:15.820: E/AndroidRuntime(32157): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
离开后方法如下:
@Override
public void bindView(View view, Context context, Cursor cursor)
{
((LinearLayout) view).addView(view);
}
这是抛出异常后的打印堆栈
06-02 00:53:37.031: E/AndroidRuntime(32327): FATAL EXCEPTION: main
06-02 00:53:37.031: E/AndroidRuntime(32327): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.ListView.measureScrapChild(ListView.java:1163)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1095)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1062)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:939)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:921)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.os.Looper.loop(Looper.java:137)
06-02 00:53:37.031: E/AndroidRuntime(32327): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-02 00:53:37.031: E/AndroidRuntime(32327): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 00:53:37.031: E/AndroidRuntime(32327): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 00:53:37.031: E/AndroidRuntime(32327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 00:53:37.031: E/AndroidRuntime(32327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 00:53:37.031: E/AndroidRuntime(32327): at dalvik.system.NativeStart.main(Native Method)