Error - You must supply a resource ID for a TextView

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

Following 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 section:

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 code that contains adapters, manifest and xml for the TextView with error follow:

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 calendar of contacts synchronized with that of Android. The section where the error occurred, is referring to the update of the name and surname of the contact in a ListView.

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

EDIT:

Here is the code for the UserRecord (where the UserItemAdapter comes from):

package br.com.contactsmanager;

public class UserRecord 
    {
    public String firstname;

    public UserRecord(String firstname) 
    {
        this.firstname = firstname;
    }
}
    
asked by anonymous 17.03.2014 / 13:46

1 answer

4

Unfortunately the part of the log that could actually reveal where the error was is 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 itself, you can see that a LinearLayout is being used where a TextView is expected, within the ArrayAdapter class.

Who creates UserItemAdapter ? Is the second parameter passed to it ( textViewResourceId ) correct? Second this documentation when creating a ArrayAdapter with three parameters, like you did, the second should be the layout ID, not the textView - and how you gave the parameter name textViewResourceId > maybe it is wrong.

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

You've already tried using 4-parameter constructor of ArrayAdapter ? I do not know if it will solve it, but it is the best suggestion that I can give in the present information. If it does not, try isolating the code snippet where the exception is occurring (using try...catch , printing in the log, something like this) to make it easier to know where it is.

    
17.03.2014 / 16:40