ClassCastException:设置微调框的提示时,android.widget.LinearLayout无法转换为android.widget.TextView

问题描述 投票:-1回答:1

正在尝试发送有关使用基本适配器加载微调器项目的微调器的提示

下面是我的微调适配器

package manu.apps.cartv6.Adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import manu.apps.cartv6.Classes.Category;
import manu.apps.cartv6.R;

public class CategoryAdapter extends BaseAdapter {

    private Context context;
    private List<Category> categoryList;

    public CategoryAdapter(Context context, List<Category> categoryList) {
        this.context = context;
        this.categoryList = categoryList;
    }

    @Override
    public int getCount() {
        return categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_spinner_items,parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Category currentCategory = (Category) getItem(position);

        viewHolder.categoryName.setText(currentCategory.getCategoryName());


        return convertView;
    }

    private class ViewHolder {
        TextView categoryId, categoryName;

        public ViewHolder(View view) {
            categoryName = view.findViewById(R.id.tv_spinner_item_name);
        }
    }

    @Override
    public boolean isEnabled(int position) {
        return position != 0;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View view = super.getDropDownView(position, convertView, parent);
        TextView tv = (TextView) view;
        if(position == 0){
            // Set the hint text color gray
            tv.setTextColor(Color.GRAY);
        }
        else {
            tv.setTextColor(Color.BLACK);
        }
        return view;
    }
}

这是我用来设置微调适配器的方法


private void setUpSelectCategorySpinner(List<Category> categoryList) {
        categoryAdapter = new CategoryAdapter(getActivity(), categoryList);
        spnSelectCategory.setAdapter(categoryAdapter);
    }

这里是我的微调器,用于选定的侦听器


spnSelectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                category = (Category) parent.getSelectedItem();

                categoryId = category.getId();
                categoryName = category.getCategoryName();

                String selectedItemText = categoryName;

                // Test on Item Selected
                //Toast.makeText(getActivity(),"Id: " + categoryId+ "\nCategoryName: " + categoryName , Toast.LENGTH_SHORT).show();

                if(position > 0){
                    // Notify the selected item text
                    Toast.makeText
                            (getActivity(), "Selected : " + selectedItemText, Toast.LENGTH_SHORT)
                            .show();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

我希望我在微调器中加载的第一项显示为提示,并出现以下错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: manu.apps.cartv6, PID: 22404
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
    at manu.apps.cartv6.Adapters.CategoryAdapter.getDropDownView(CategoryAdapter.java:78)
    at androidx.appcompat.widget.AppCompatSpinner$DropDownAdapter.getDropDownView(AppCompatSpinner.java:740)
    at androidx.appcompat.widget.AppCompatSpinner$DropDownAdapter.getView(AppCompatSpinner.java:734)
    at androidx.appcompat.widget.AppCompatSpinner.compatMeasureContentWidth(AppCompatSpinner.java:574)
    at androidx.appcompat.widget.AppCompatSpinner$DropdownPopup.computeContentWidth(AppCompatSpinner.java:1010)
    at androidx.appcompat.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:1037)
    at androidx.appcompat.widget.AppCompatSpinner.showPopup(AppCompatSpinner.java:600)
    at androidx.appcompat.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:444)
    at android.view.View.performClickInternal(View.java:6631)
    at android.view.View.access$3100(View.java:790)
    at android.view.View$PerformClick.run(View.java:26187)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7625)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

下面是我的xml,微调器位于线性布局内的相对布局内


 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="1dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginStart="5dp">

                    <TextView
                        android:id="@+id/tv_select_category"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/select_a_category_with_colon"
                        style="@style/TextAppearance.AppCompat.Body1"
                        android:layout_alignParentStart="true"
                        android:layout_centerInParent="true"
                        android:layout_margin="1dp"/>

                    <Spinner
                        android:id="@+id/spn_select_category"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_toEndOf="@+id/tv_select_category"
                        android:layout_alignParentEnd="true"
                        android:layout_centerInParent="true" />


                </RelativeLayout>

我的布局微调项


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

    <TextView
        android:id="@+id/tv_spinner_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spinner Item Name"
        style="@style/TextAppearance.AppCompat.Small"
        android:layout_margin="5dp"/>

</LinearLayout>

提前感谢

android android-spinner
1个回答
0
投票

您在这里遇到错误。

  TextView tv = (TextView) view;

[viewlinearlayout,而您要转换为textview

更改为

  TextView tv = (TextView) view.findViewById(R.id.yourtextview);
© www.soinside.com 2019 - 2024. All rights reserved.