java – IllegalStateException: ArrayAdapter requires the resource ID to be a TextView-ThrowExceptions

Exception or error:

I ran my code once and it was fine, I ran it again and now it crashes every time. LogCat gave me this:

04-11 03:15:57.293: D/dalvikvm(344): GC_EXTERNAL_ALLOC freed 68K, 52% free 2588K/5379K,   external 1907K/2137K, paused 65ms
04-11 03:15:57.497: E/ArrayAdapter(344): You must supply a resource ID for a TextView
04-11 03:15:57.497: D/AndroidRuntime(344): Shutting down VM
04-11 03:15:57.497: W/dalvikvm(344): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-11 03:15:57.573: E/AndroidRuntime(344): FATAL EXCEPTION: main
04-11 03:15:57.573: E/AndroidRuntime(344): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ListView.onMeasure(ListView.java:1127)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.View.measure(View.java:8313)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.os.Looper.loop(Looper.java:123)
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 03:15:57.573: E/AndroidRuntime(344):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344):  at java.lang.reflect.Method.invoke(Method.java:507)
04-11 03:15:57.573: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 03:15:57.573: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 03:15:57.573: E/AndroidRuntime(344):  at dalvik.system.NativeStart.main(Native Method)
04-11 03:15:57.573: E/AndroidRuntime(344): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
04-11 03:15:57.573: E/AndroidRuntime(344):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
04-11 03:15:57.573: E/AndroidRuntime(344):  ... 35 more
04-11 03:17:31.694: I/Process(344): Sending signal. PID: 344 SIG: 9

I have three activities:

package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuizMenuActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        ListView menuList = (ListView) findViewById(R.id.listView1);

        String[] items = { getResources().getString(R.string.itemplay),
                getResources().getString(R.string.itemscores),
                getResources().getString(R.string.itemsettings),
                getResources().getString(R.string.itemhelp) };
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
        menuList.setAdapter(adapt);

        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {

                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id

                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();

                if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
                    // Launch the Game Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
                    // Launch the Help Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
                    // Launch the Settings Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
                    // Launch the Scores Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
                }

            }
        });


    }


}

this one:

package com.androidbook.triviaquiz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class QuizMenuActivity extends QuizActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        ListView menuList = (ListView) findViewById(R.id.listView1);

        String[] items = { getResources().getString(R.string.itemplay),
                getResources().getString(R.string.itemscores),
                getResources().getString(R.string.itemsettings),
                getResources().getString(R.string.itemhelp) };
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
        menuList.setAdapter(adapt);

        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {

                // Note: if the list was built "by hand" the id could be used.
                // As-is, though, each item has the same id

                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();

                if (strText.equalsIgnoreCase(getResources().getString(R.string.itemplay))) {
                    // Launch the Game Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemhelp))) {
                    // Launch the Help Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemsettings))) {
                    // Launch the Settings Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(R.string.itemscores))) {
                    // Launch the Scores Activity
                    startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
                }

            }
        });


    }


}

and this one:

package com.androidbook.triviaquiz;


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class QuizGameActivity extends QuizActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.gameoptions, menu);
        menu.findItem(R.id.help_menu_item).setIntent(new Intent(this, QuizHelpActivity.class));
        menu.findItem(R.id.settings_menu_item).setIntent(new Intent(this, QuizSettingsActivity.class));
        return true;
    }
     @Override
       public boolean onOptionsItemSelected(MenuItem item) {
          super.onOptionsItemSelected(item);
          startActivity(item.getIntent());
           return true;
        }
}

Can someone tell me what is wrong with this code? Like I said before, all I did was restart it. Are these the only things that you need to figure it out, maybe the manifest?

EDIT: Res/layout/menu

<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: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=".QuizSplashActivity" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/th"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#000000" >

            <ImageView
                android:id="@+id/ImageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/quizicon" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="MAIN MENU"
                android:textColor="#ffffff"
                android:textSize="20pt" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/quizicon" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:gravity="center">



            </ListView>

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>
How to solve:

You have to specify your TextView ID which you used in your R.layout.menu

Change this :

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items); 

To this :

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, R.id.YOUR_TEXT_VIEW, items); 

###

The ArrayAdapter constructor you are using expects a layout resource that consists of a TextView only. It will try to bind the data of the list to the TextView. Instead it finds a RelativeLayout and fails.

It looks like you gave it your Activity’s layout. Perhaps you meant to give it a different layout resource? If not, you should use a different constructor that takes both a layout resource and the id of a textview within that layout.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.list_item, R.layout.text, items);

###

the exception is quite clear imho : ArrayAdapter requires the resource ID to be a TextView, see docs:

A ListAdapter that manages a ListView backed by an array of arbitrary
objects.

By default this class expects that the provided resource id references
a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

###

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);

you are creating an array adapter with the contructor where u are just giving 3 parameters- context, textview resourceId and the listItems.

So instead of the layout, it expects the textview id where data need to be set.

In case u need to send the layout id also then use another constructor and pass textviewresourceId as 0.

like this

mAdapter = new ArrayAdapter(mContext, R.layout.custom_layout, 0, itemsList);

If u have a textview ResourceId you can pass the resourceid in place of 0

Hope this should work for u.

###

You can use “layout.support_simple_spinner_dropdown_item” just as stated below.

YourArrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item);

Leave a Reply

Your email address will not be published. Required fields are marked *