title: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
body: <p>First of all, I know there are many questions same as mine. I have read all of them and did not find out the answer for my situation yet.</p>
  
  <p><strong>My Situation</strong></p>
  
  <p>I'm trying to implement <code>LetterAvatarView</code> like the Gmail. I used the <a href="https://github.com/nkzawa/socket.io-android-chat/" rel="nofollow noreferrer">SocketIO's sample project</a>, and modified it's <code>item_message.xml</code> by adding the <code>LetterAvatarView</code>. Below is the result.</p>
  
  <p><a href="https://i.stack.imgur.com/ak5Zu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ak5Zu.png" alt="enter image description here"></a> </p>
  
  <p>Here is <code>item_message.xml</code></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center_vertical"
      android:paddingTop="@dimen/spacing"&gt;
  
      &lt;com.github.nkzawa.socketio.androidchat.LetterAvatarView &lt;--- error occur
          android:id="@+id/la_avatar"
          android:layout_width="50dp"
          android:layout_height="50dp" /&gt;
  
      &lt;TextView
          android:id="@+id/username"
          style="?android:textAppearanceMedium"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:singleLine="true"
          android:textColor="?android:textColorPrimary"
          android:textStyle="bold" /&gt;
  
      &lt;TextView
          android:id="@+id/message"
          style="?android:textAppearanceMedium"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:paddingLeft="@dimen/spacing"
          android:paddingRight="@dimen/spacing"
          android:singleLine="true"
          android:textColor="?android:textColorPrimary" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My <code>LetterAvatarView</code> class:</p>
  
  <pre><code>public class LetterAvatarView extends View {
  
      private Paint mTextPaint;
      private Paint mBackgroundPaint;
      private String[] colors = new String[]{
          "#e7e7e7", "#b6cff5", "#98d7e4", "#e3d7ff", "#fbd3e0", "#f2b2a8", "#c2c2c2", "4986e7",
              "#2da2bb", "#b99aff", "#f691b2", "#fb4c2f", "#ffc8af", "#ffdeb5", "#fbe983", "#fdedc1",
              "#b3efd3", "#a2dcc1", "#ff7537", "#ffad46", "#ebdbde", "#cca6ac", "#42d692", "#16a765"
      };
      private int radius;
      private int centerX;
      private int centerY;
      private String firstCharacter = "";
  
      public LetterAvatarView(Context context) {
          super(context);
          init();
      }
  
      public LetterAvatarView(Context context, AttributeSet attrs) {
          super(context, attrs);
          init();
      }
  
      public LetterAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
          super(context, attrs, defStyleAttr);
          init();
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          canvas.drawCircle(centerX, centerY, radius, mBackgroundPaint);
          drawTextCentred(canvas, firstCharacter, centerX, centerY, mTextPaint);
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
          radius = w / 2;
          centerX = w / 2;
          centerY = h / 2;
          mTextPaint.setTextSize(radius);
      }
  
      private void init(){
          mTextPaint = new Paint();
          mBackgroundPaint = new Paint();
          Random random = new Random();
          int backgroundColor = Color.parseColor(colors[random.nextInt(colors.length)]);
          mBackgroundPaint.setColor(backgroundColor);
          mBackgroundPaint.setStyle(Paint.Style.FILL);
          mBackgroundPaint.setAntiAlias(true);
          mTextPaint.setColor(Color.WHITE);
          mTextPaint.setAntiAlias(true);
          mTextPaint.setTextAlign(Paint.Align.CENTER);
      }
  
      public void setText(String text){
          if(!TextUtils.isEmpty(text)) {
              firstCharacter = text.substring(0, 1);
              firstCharacter = firstCharacter.toUpperCase();
          }
      }
  
      public void drawTextCentred(Canvas canvas, String text, float cx, float cy, Paint paint){
          Rect textBounds = new Rect();
          paint.getTextBounds(text, 0, text.length(), textBounds);
          canvas.drawText(text, cx, cy - textBounds.exactCenterY(), paint);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE 1</strong>
  Here my adapter class</p>
  
  <pre><code>public class MessageAdapter extends RecyclerView.Adapter&lt;MessageAdapter.ViewHolder&gt; {
  
  private List&lt;Message&gt; mMessages;
  private int[] mUsernameColors;
  
  public MessageAdapter(Context context, List&lt;Message&gt; messages) {
      mMessages = messages;
      mUsernameColors = context.getResources().getIntArray(R.array.username_colors);
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      int layout = -1;
      switch (viewType) {
      case Message.TYPE_MESSAGE:
          layout = R.layout.item_message;
          break;
      case Message.TYPE_LOG:
          layout = R.layout.item_log;
          break;
      case Message.TYPE_ACTION:
          layout = R.layout.item_action;
          break;
      }
      View v = LayoutInflater
              .from(parent.getContext())
              .inflate(layout, parent, false);
      return new ViewHolder(v);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int position) {
      Message message = mMessages.get(position);
      viewHolder.setMessage(message.getMessage());
      viewHolder.setUsername(message.getUsername());
  }
  
  @Override
  public int getItemCount() {
      return mMessages.size();
  }
  
  @Override
  public int getItemViewType(int position) {
      return mMessages.get(position).getType();
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
      private TextView mUsernameView;
      private TextView mMessageView;
      private LetterAvatarView mAvatarView;
  
      public ViewHolder(View itemView) {
          super(itemView);
  
          mUsernameView = (TextView) itemView.findViewById(R.id.username);
          mMessageView = (TextView) itemView.findViewById(R.id.message);
          mAvatarView = (LetterAvatarView) itemView.findViewById(R.id.la_avatar);
      }
  
      public void setUsername(String username) {
          if (null == mUsernameView) return;
          mUsernameView.setText(username);
          mUsernameView.setTextColor(getUsernameColor(username));
          if(mAvatarView != null) mAvatarView.setText(username);
      }
  
      public void setMessage(String message) {
          if (null == mMessageView) return;
          mMessageView.setText(message);
      }
  
      private int getUsernameColor(String username) {
          int hash = 7;
          for (int i = 0, len = username.length(); i &lt; len; i++) {
              hash = username.codePointAt(i) + (hash &lt;&lt; 5) - hash;
          }
          int index = Math.abs(hash % mUsernameColors.length);
          return mUsernameColors[index];
      }
  }
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong> <code>Message</code> class</p>
  
  <pre><code>public class Message {
  
  public static final int TYPE_MESSAGE = 0;
  public static final int TYPE_LOG = 1;
  public static final int TYPE_ACTION = 2;
  
  private int mType;
  private String mMessage;
  private String mUsername;
  
  private Message() {}
  
  public int getType() {
      return mType;
  };
  
  public String getMessage() {
      return mMessage;
  };
  
  public String getUsername() {
      return mUsername;
  };
  
  
  public static class Builder {
      private final int mType;
      private String mUsername;
      private String mMessage;
  
      public Builder(int type) {
          mType = type;
      }
  
      public Builder username(String username) {
          mUsername = username;
          return this;
      }
  
      public Builder message(String message) {
          mMessage = message;
          return this;
      }
  
      public Message build() {
          Message message = new Message();
          message.mType = mType;
          message.mUsername = mUsername;
          message.mMessage = mMessage;
          return message;
      }
  }
  }
  </code></pre>
  
  <p><strong>Problem</strong>
  My code some time work. But sometime it crashed with:</p>
  
  <pre><code>android.view.InflateException: Binary XML file line #8: Error inflating class &lt;unknown&gt;
  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
  at com.github.nkzawa.socketio.androidchat.MessageAdapter.onCreateViewHolder(MessageAdapter.java:39)
  at com.github.nkzawa.socketio.androidchat.MessageAdapter.onCreateViewHolder(MessageAdapter.java:13)
  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:508)
  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  at android.view.View.layout(View.java:14289)
  at android.view.ViewGroup.layout(ViewGroup.java:4562)
  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
  at android.view.Choreographer.doFrame(Choreographer.java:532)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
  at android.os.Handler.handleCallback(Handler.java:730)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5103)
  at java.lang.reflect.Method.invokeNative(Native Method)
  </code></pre>
  
  <p><strong>How can I fix it?</strong></p>