Error - You must Supply a Resource ID for a Textview

Asked

Viewed 313 times

0

03-17 08:31:32.283: E/ArrayAdapter(1085): You must supply a resource ID for a TextView
03-17 08:31:32.283: D/AndroidRuntime(1085): Shutting down VM
03-17 08:31:32.283: W/dalvikvm(1085): threadid=1: thread exiting with uncaught exception (group=0xb4a39b90)
03-17 08:31:32.343: E/AndroidRuntime(1085): FATAL EXCEPTION: main
03-17 08:31:32.343: E/AndroidRuntime(1085): Process: br.com.contactsmanager, PID: 1085
03-17 08:31:32.343: E/AndroidRuntime(1085): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.ListView.onMeasure(ListView.java:1175)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.View.measure(View.java:16458)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.View.measure(View.java:16458)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.View.measure(View.java:16458)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.View.measure(View.java:16458)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.View.measure(View.java:16458)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.os.Handler.handleCallback(Handler.java:733)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.os.Looper.loop(Looper.java:137)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invoke(Method.java:515)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-17 08:31:32.343: E/AndroidRuntime(1085):     at dalvik.system.NativeStart.main(Native Method)
03-17 08:31:32.343: E/AndroidRuntime(1085): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
03-17 08:31:32.343: E/AndroidRuntime(1085):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
03-17 08:31:32.343: E/AndroidRuntime(1085):     ... 38 more

Below are the errors that appear in Logcat when starting the application in the emulator. The error message displayed on the screen is "Unfortunately, Contacts Manager has stopped."

I believe the error is in this part:

public View getView(int position, View convertView, ViewGroup parent) 
{
    // Recupera a pessoa da posição atual
    Contact p = list.get(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.contact_table, null);

    // Atualiza o valor do TextView
    TextView firstname = (TextView) view.findViewById(R.id.showname);
    firstname.setText(p.firstname);

    return view;
}

Anyway, the codes that contain Adapters, manifest and xml referring to Textview with error are attached:

Contactlistadapter.java

package br.com.contactsmanager;

import java.util.List;

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


public class ContactListAdapter extends BaseAdapter 
{

    private Context context;
    private List<Contact> list;

    public ContactListAdapter(Context context, List<Contact> list) 
    {
        this.context = context;
        this.list = list;
    }

    public int getCount() 
    {
        return list.size();
    }

    public Object getItem(int position) 
    {
        return list.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // Recupera a pessoa da posição atual
        Contact p = list.get(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.contact_table, null);

        // Atualiza o valor do TextView
        TextView firstname = (TextView) view.findViewById(R.id.showname);
        firstname.setText(p.firstname);

        return view;
    }

}

Useritemadapter.java

package br.com.contactsmanager;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class UserItemAdapter extends ArrayAdapter<UserRecord> 
{
    private ArrayList<UserRecord> users;

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) 
    {
        super(context, textViewResourceId, users);
        this.users = users;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
            v = vi.inflate(R.layout.contact_table, null);
        }

        UserRecord user = users.get(position);
        if (user != null) {
            TextView firstname = (TextView) v.findViewById(R.id.showname);
            //TextView lastname = (TextView) v.findViewById(R.id.showlastname);

            if (firstname != null) 
            {
                firstname.setText(user.firstname);
            }

            /*if(lastname != null) 
            {
                lastname.setText("Sobrenome: " + user.lastname );
            }*/
        }
        return v;
    }
}

Androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.contactsmanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.READ_CONTACTS">
    </uses-permission>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />  

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppHolo"
        android:allowBackup="true" >

        <activity 
            android:name="br.com.contactsmanager.ReadContacts"
            class=".ReadContacts" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />
                <action android:name="android.intent.action.GET_CONTENT" />             
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name="br.com.contactsmanager.AndroidCamera"
            class=".AndroidCamera" >
            <intent-filter> 
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.ALTERNATIVE" />
            </intent-filter>
        </activity>

        <activity android:name=".contactsmanager.contactregister" ></activity>

        <activity android:name="EditContact"></activity>

        <activity android:name="SearchContact"></activity>    

    </application>

</manifest>

contact_table.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:layout_width="wrap_content"
    android:paddingBottom="5px"
    android:paddingTop="5px"
    android:paddingLeft="5px">    

    <ImageView 
        android:id="@+id/avatar"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dip"
        android:src="@drawable/avatar" >
    </ImageView>

    <TextView 
    android:id="@+id/showname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">
    </TextView>
</LinearLayout>

To make it clear, the idea of the project is a contact schedule synchronized with that of Android. The excerpt in which the error appeared, refers to the update of the contact’s name and last name in a Listview.

Then I will complete the update of the photo as well. If someone already knows how to do this, or if there is a topic with a solution, I would appreciate.

EDIT.:

Follow the Userrecord code (where the Useritemadapter comes from):

package br.com.contactsmanager;

public class UserRecord 
    {
    public String firstname;

    public UserRecord(String firstname) 
    {
        this.firstname = firstname;
    }
}

1 answer

4


Unfortunately the part of the log that could actually reveal where the error is got truncated:

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
    ... 38 more

But by the exception in itself, you can already see that a LinearLayout is being used where one expects TextView, within the class ArrayAdapter.

Who creates the UserItemAdapter? The second parameter passed to him (textViewResourceId) is that correct? Second that documentation when creating a ArrayAdapter with three parameters, as you did, the second should be the layout ID, not the textView - and how you named the parameter textViewResourceId I imagine that maybe he is wrong.

(although this does not explain the error message - it should be the other way around, a ClassCastException of TextView for LinearLayout)

Have you tried using the constructor of 4 parameters of ArrayAdapter? I don’t know if it’ll do the trick, but it’s the best suggestion I can make in front of the current information. If not, try to isolate the code snippet where the exception is occurring (using try...catch, printing in the log, something like that) to make it easier to know where it is.

  • 1

    I edited the post with the code that creates the Useritemadapter list (Userrecord), fixed the textViewResourceId for the Textview id, and added the 4 parameter constructor of Arrayadapter. Still the error persists. Now I will try to leave the part that is with commented error, to see if it really is there. Anyway, I appreciate the help, because I didn’t know about this builder, and I will continue in search of the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.