【问题标题】:can not resolve symbol 'from' Error in Android Studio无法解析 Android Studio 中的符号“来自”错误
【发布时间】:2017-12-10 00:36:25
【问题描述】:

中无法解析符号'from'
LayoutInflater inflater = new LayoutInflater.from(parent.getContext());

这是我的完整课程:

package ir.sangram.sangram.chat;

import android.content.Context;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import ir.sangram.sangram.R;

/**
 * Created by HOW on 07/04/2017.
 */
class ChatListViewHolder extends RecyclerView.ViewHolder{
    public CircleImageView profile_picture;
    public TextView user_name, last_chat, unread_messages;
    public ChatListViewHolder(View itemView) {
        super(itemView);
        profile_picture = (CircleImageView) itemView.findViewById(R.id.profile_picture);
        user_name = (TextView) itemView.findViewById(R.id.user_name);
        last_chat = (TextView) itemView.findViewById(R.id.last_chat);
        unread_messages = (TextView) itemView.findViewById(R.id.unread_messages);
    }
}
public class ChatListAdapter extends RecyclerView.Adapter<ChatListViewHolder>{
    private List<ChatListData> chat_list = new ArrayList<ChatListData>();

    public ChatListAdapter(List<ChatListData> chat_list) {
        this.chat_list = chat_list;
    }

    @Override
    public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
        //LayoutInflater inflater;// = new LayoutInflater.from(parent.getContext());
        //inflater = ( LayoutInflater )parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);//!!!!!!!!!!
        View itemView = inflater.inflate(R.layout.chat_list_row,parent,false);
        return new ChatListViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ChatListViewHolder holder, int position) {
        holder.profile_picture.setImageResource(chat_list.get(position).getProfile_picture_id());
        holder.user_name.setText(chat_list.get(position).getUser_name());
        holder.unread_messages.setText(chat_list.get(position).getUnread_messages());
        holder.last_chat.setText(chat_list.get(position).getLast_chat());
    }

    @Override
    public int getItemCount() {
        return chat_list.size();
    }
}

我能做什么? 请帮我 谢谢

【问题讨论】:

  • 在不知情的情况下:重启android studio。检查 android.view.LayoutInflater 的文档。检查您的代码是否有错误的方括号和其他语法失败。
  • 删除

标签: java android android-recyclerview


【解决方案1】:

试试这个方法我的朋友它会帮助你

  View view = LayoutInflater.from(context).inflate(R.layout.cust_invitation, parent, false);
  ViewHolder viewHolder = new ViewHolder(view);
  return viewHolder;

【讨论】:

    【解决方案2】:

    您调用LayoutInflater.from 就像调用构造函数一样:

    LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
    

    这是构造函数调用语法 (new) 和方法调用语法 (.from) 的混合体。如果LayoutInflater.from 是一个静态嵌套类,它会起作用,但它不是......它只是静态方法。所以你想要:

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    

    【讨论】:

      【解决方案3】:

      不需要“new”关键字。试试下面的代码:

      @Override
          public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      
              LayoutInflater inflater =  LayoutInflater.from(parent.getContext());
              View itemView = inflater.inflate(R.layout.chat_list_row,parent,false);
              return new ChatListViewHolder(itemView);
          }
      

      【讨论】:

        【解决方案4】:

        LayoutInflater.from() 是一个static 方法,要访问static 方法,您无需使用new 关键字创建instance

        用途:

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        

        代替:

        LayoutInflater inflater = new LayoutInflater.from(parent.getContext());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-23
          • 2019-07-29
          • 1970-01-01
          • 1970-01-01
          • 2015-03-03
          • 2015-03-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多