0

我创建了一个 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)
4

3 回答 3

4

您不能将视图放在其他视图中。您需要一个 ViewGroup 来执行此操作,因此会出现错误。如果您在布局 xml 文件中切换回 LinearLayout,错误应该会消失。

于 2012-06-01T20:54:18.973 回答
1

尝试这个:

View view = inflater.inflate(R.layout.competitor_autocomplete_list_item, parent, false);

编辑

我错过了你从线性布局改变。Renard 是正确的,将其改回(不要尝试强制转换),它应该可以工作。

于 2012-06-01T20:58:52.453 回答
0

我为子孙后代记录了这一点。

误导我的是我不明白我必须复制我newView()bindView().

我看到人们TextView像这样绑定一个单曲:((TextView) view).setText(item);并且认为我必须做同样的事情以防我通过 aView并且因为你不能setText()View我做了这样的事情:((LinearLayout) view).addView(view);

好吧,我宁愿忘记这件事,即使发生在我身上。如果您有兴趣在 CursorAdapter 中使用 LinearLayout,这是正确的代码:

@Override
public View bindView(Context context, Cursor cursor, ViewGroup parent)
{
  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(TournamentConstants.TblStocks.COLUMN_NAME)); 
  String stockSymbol = cursor.getString(cursor.getColumnIndex(TournamentConstants.TblStocks.COLUMN_EXTERNAL_ID)); 

  stockNameTextView.setText(stockName);
  stockSymbolTextView.setText(stockSymbol);       
}
于 2012-06-04T08:54:48.787 回答