1
resposta

AppCompatSpinner cannot be cast to extView

Estou tentando utilizar o Spinner com valores fixos, porém, quando o app é redirecionado para o .xml que utiliza o Spinner, o app dá o seguinte erro:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: br.com.ippontadareia.cancioneiroippa, PID: 3913
                  java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
                      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
                      at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
                      at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
                      at android.widget.Spinner.onMeasure(Spinner.java:507)
                      at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:419)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                      at android.view.View.measure(View.java:17547)
                      at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1260)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.widget.ScrollView.onMeasure(ScrollView.java:337)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
                      at android.view.View.measure(View.java:17547)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                      at android.view.Choreographer.doFrame(Choreographer.java:550)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatSpinner cannot be cast to android.widget.TextView

.xml com a tag Spinner:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_search_pop_up"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="br.com.ippontadareia.cancioneiroippa.SearchPopUpActivity"
        android:orientation="vertical">

        <Spinner
            android:id="@+id/search_opitions_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/search_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/do_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

Activity:

   private static final String SEARCH_BY_NUMBER = "Buscar por Número";
   private static final String SEARCH_BY_TITLE = "Buscar por Título";
   private static final String SEARCH_BY_LYRICS = "Buscar por Letra";

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_pop_up);
        getWindow().setLayout(ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT);

        List<String> spinnerSearchOptions = new ArrayList<String>();
        spinnerSearchOptions.add(SEARCH_BY_NUMBER);
        spinnerSearchOptions.add(SEARCH_BY_TITLE);
        spinnerSearchOptions.add(SEARCH_BY_LYRICS);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchPopUpActivity.this,
                R.layout.activity_search_pop_up, R.id.search_opitions_spinner, spinnerSearchOptions);

        final Spinner spinnerOptions = (Spinner) findViewById(R.id.search_opitions_spinner);
        spinnerOptions.setAdapter(adapter);

Como faço para corrigir esse erro e a activity ser exibida com o Spinner?

1 resposta

Oi Lucas, tudo bem ?

Aqui você está com o problema que vimos no curso, quando queremos criar uma listagem com itens personalizados, para o seu Spinner terá que fazer o mesmo, criar um Adapter personalizado.

Caso contrário, você pode usar o layout padrão que o ArrayAdapter usa por trás dos panos : simple_list_item_1.

Espero ter ajudado.

Abraços