Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Retrieving and Viewing Messages Creating a Custom List Adapter for Messages

errors trying get MessageAdapter to work

hi,

i try to apply the message adapter on the list but get full of errors. i got that the problem is in the xml but what is it? maybe im wrong...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/msgIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
      />

    <TextView
        android:id="@+id/senderLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

MsgAdapter.java

package com.benbasha.sedu;

import java.util.List;

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

import com.parse.ParseObject;



public class MsgAdapter extends ArrayAdapter<ParseObject> {

    protected Context mContext;
    protected List<ParseObject> mMsg;

    public MsgAdapter(Context context, List<ParseObject> messages) {
        super(context, R.layout.message_item, messages);
        mContext = context;
        mMsg = messages;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
            holder = new ViewHolder();
            holder.iconImageView = (ImageView) convertView.findViewById(R.id.msgIcon);
            holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ParseObject msg = mMsg.get(position);

        if (msg.getString(Constants.KEY_FILE_TYPE).equals(Constants.TYPE_VIDEO))
            holder.iconImageView.setImageResource(R.drawable.ic_action_picture);
        else 
            holder.iconImageView.setImageResource(R.drawable.ic_action_camera);
        holder.nameLabel.setText(msg.getString(Constants.KEY_SENDER_NAME));

        return convertView;
    }

    private static class ViewHolder {
        ImageView iconImageView;
        TextView nameLabel;
    }
}

The ERRORs

06-05 22:33:58.431: E/AndroidRuntime(1621): FATAL EXCEPTION: main
06-05 22:33:58.431: E/AndroidRuntime(1621): Process: com.benbasha.sedu, PID: 1621
06-05 22:33:58.431: E/AndroidRuntime(1621): android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at com.benbasha.sedu.MsgAdapter.getView(MsgAdapter.java:34)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.ListView.fillDown(ListView.java:691)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.ListView.fillFromTop(ListView.java:752)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.ListView.layoutChildren(ListView.java:1630)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.layout(View.java:14817)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.Choreographer.doFrame(Choreographer.java:544)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.os.Handler.handleCallback(Handler.java:733)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.os.Looper.loop(Looper.java:136)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at java.lang.reflect.Method.invoke(Method.java:515)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at dalvik.system.NativeStart.main(Native Method)
06-05 22:33:58.431: E/AndroidRuntime(1621): Caused by: java.lang.reflect.InvocationTargetException
06-05 22:33:58.431: E/AndroidRuntime(1621):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-05 22:33:58.431: E/AndroidRuntime(1621):     ... 54 more
06-05 22:33:58.431: E/AndroidRuntime(1621): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464)
06-05 22:33:58.431: E/AndroidRuntime(1621):     at android.view.View.<init>(View.java:3569

i google for answer but cant find good one for me.

thanks alot for your help.

1 Answer

ahhhh finally get it to work!

i use the wrong section for the demensions that cause the app crash =\

now everything works!

thanks