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
Seguem acima os erros que surgem no LogCat ao iniciar a aplicação no emulador. A mensagem de erro exibida na tela é "Unfortunately, Contacts Manager has stopped."
Acredito que o erro esteja nesse trecho:
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;
}
De qualquer maneira, seguem em anexo os códigos que contém adapters, manifest e o xml referente ao TextView com erro:
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>
Para deixar claro, a ideia do projeto é uma agenda de contatos sincronizada com a do Android. O trecho em que o erro surgiu, é referente à atualização do nome e sobrenome do contato em uma ListView.
Em seguida implementarei a atualização da foto também. Caso alguém já saiba como fazer isso, ou se existe algum tópico com uma solução, eu agradeceria.
EDIT.:
Segue o código do UserRecord (de onde vem o UserItemAdapter):
package br.com.contactsmanager;
public class UserRecord
{
public String firstname;
public UserRecord(String firstname)
{
this.firstname = firstname;
}
}