Во -первых, я знаю, что есть много вопросов, так же, как у меня. Я прочитал их все и еще не узнал ответ на мою ситуацию. Я использовал пример проекта Socketio и изменил его item_message.xml , добавив ettrivavatarview . Ниже приведен результат. < /P>
my letgavatarview < /code> class: < /p>
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>
[b] Обновление 1 < /strong>
здесь мой класс адаптера < /p>
public class MessageAdapter extends RecyclerView.Adapter {
private List mMessages;
private int[] mUsernameColors;
public MessageAdapter(Context context, List 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 < len; i++) {
hash = username.codePointAt(i) + (hash
Обновление 2 < /strong> message < /code> class < /p>
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>
Проблема < /strong>
мой код некоторое время работы. Но иногда он разбился с: < /p>
android.view.InflateException: Binary XML file line #8: Error inflating class
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)
Во -первых, я знаю, что есть много вопросов, так же, как у меня. Я прочитал их все и еще не узнал ответ на мою ситуацию. Я использовал пример проекта Socketio и изменил его item_message.xml , добавив ettrivavatarview . Ниже приведен результат. < /P>
@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 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 < len; i++) { hash = username.codePointAt(i) + (hash
Обновление 2 < /strong> message < /code> class < /p>
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;
Проблема < /strong> мой код некоторое время работы. Но иногда он разбился с: < /p>
android.view.InflateException: Binary XML file line #8: Error inflating class 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]
В некоторых телефонах происходит следующая ошибка. Я не могу определить проблему, просмотрев детали журнала. Детали журнала и содержимое файла XML могут просмотреть ниже.
java.lang.RuntimeException: Unable to start activity...
В некоторых телефонах происходит следующая ошибка. Я не могу определить проблему, просмотрев детали журнала. Детали журнала и содержимое файла XML могут просмотреть ниже.
java.lang.RuntimeException: Unable to start activity...
Я новичок в изучении Android. Я буду коротким. Я получаю ошибку, как упомянуто внизу. Я добавил фрагмент через XML. Я искал решения, но до сих пор ни один из них не работал. Где я ошибаюсь? Могу я получить объяснение, почему это происходит?...