Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试调用虚拟方法';java.lang.String example.asuspc.prospect.model.UserModel.getPhoto_profile()';关于空对象引用_Android - Fatal编程技术网

Android 尝试调用虚拟方法';java.lang.String example.asuspc.prospect.model.UserModel.getPhoto_profile()';关于空对象引用

Android 尝试调用虚拟方法';java.lang.String example.asuspc.prospect.model.UserModel.getPhoto_profile()';关于空对象引用,android,Android,我对Android很陌生,但我已经多次遇到这个错误,只是这次我没有解决它的线索。这是我目前正在开发的一个即时聊天应用程序的示例代码 首先是错误: 01-24 18:57:22.267 30531-30531/example.asuspc.prospect E/AndroidRuntime: FATAL EXCEPTION: main Process:

我对Android很陌生,但我已经多次遇到这个错误,只是这次我没有解决它的线索。这是我目前正在开发的一个即时聊天应用程序的示例代码

首先是错误:

01-24 18:57:22.267 30531-30531/example.asuspc.prospect E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: example.asuspc.prospect, PID: 30531
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String example.asuspc.prospect.model.UserModel.getPhoto_profile()' on a null object reference
                                                                             at example.asuspc.prospect.ChatFirebaseAdapter.populateViewHolder(ChatFirebaseAdapter.java:90)
                                                                             at example.asuspc.prospect.ChatFirebaseAdapter.populateViewHolder(ChatFirebaseAdapter.java:28)
                                                                             at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:177)
                                                                             at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
                                                                             at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
                                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                             at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                             at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                             at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                             at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:549)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                             at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                             at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                             at android.view.View.layout(View.java:15761)
                                                                             at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                             at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2090)
                                                                             at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1847)
                                                                             at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
                                                                             at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
                                                                             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                             at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                             at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Lo
有两个示例代码触发此错误:

1) ChatFirebaseAdapter.java

public class ChatFirebaseAdapter extends FirebaseRecyclerAdapter<ChatModel,ChatFirebaseAdapter.MyChatViewHolder> {

    private static final int RIGHT_MSG = 0;
    private static final int LEFT_MSG = 1;
    private static final int RIGHT_MSG_IMG = 2;
    private static final int LEFT_MSG_IMG = 3;

    private ClickListenerChatFirebase mClickListenerChatFirebase;

    private String nameUser;



    public ChatFirebaseAdapter(DatabaseReference ref, String nameUser,ClickListenerChatFirebase mClickListenerChatFirebase) {
        super(ChatModel.class, R.layout.item_message_left, ChatFirebaseAdapter.MyChatViewHolder.class, ref);
        this.nameUser = nameUser;
        this.mClickListenerChatFirebase = mClickListenerChatFirebase;
    }

    @Override
    public MyChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == RIGHT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right,parent,false);
            return new MyChatViewHolder(view);
        }else if (viewType == LEFT_MSG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left,parent,false);
            return new MyChatViewHolder(view);
        }else if (viewType == RIGHT_MSG_IMG){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_right_img,parent,false);
            return new MyChatViewHolder(view);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_left_img,parent,false);
            return new MyChatViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        ChatModel model = getItem(position);
        if (model.getMapModel() != null){
            if (model.getUserModel().getName().equals(nameUser)){
                return RIGHT_MSG_IMG;
            }else{
                return LEFT_MSG_IMG;
            }
        }else if (model.getFile() != null){
            if (model.getFile().getType().equals("img") && model.getUserModel().getName().equals(nameUser)){
                return RIGHT_MSG_IMG;
            }else{
                return LEFT_MSG_IMG;
            }
        }else if (model.getUserModel() !=null){
            if (model.getUserModel().getName().equals(nameUser));
            return RIGHT_MSG;
        }else{
            return LEFT_MSG;
        }
    }

    @Override
    protected void populateViewHolder(MyChatViewHolder viewHolder, ChatModel model, int position) {
        viewHolder.setIvUser(model.getUserModel().getPhoto_profile());
        viewHolder.setTxtMessage(model.getMessage());
        viewHolder.setTvTimestamp(model.getTimeStamp());
        viewHolder.tvIsLocation(View.GONE);
        if (model.getFile() != null){
            viewHolder.tvIsLocation(View.GONE);
            viewHolder.setIvChatPhoto(model.getFile().getUrl_file());
        }else if(model.getMapModel() != null){
            viewHolder.setIvChatPhoto(example.asuspc.prospect.Util.local(model.getMapModel().getLatitude(),model.getMapModel().getLongitude()));
            viewHolder.tvIsLocation(View.VISIBLE);
        }
    }

    public class MyChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tvTimestamp,tvLocation;
        EmojiconTextView txtMessage;
        ImageView ivUser,ivChatPhoto;

        public MyChatViewHolder(View itemView) {
            super(itemView);
            tvTimestamp = (TextView)itemView.findViewById(R.id.timestamp);
            txtMessage = (EmojiconTextView)itemView.findViewById(R.id.txtMessage);
            tvLocation = (TextView)itemView.findViewById(R.id.tvLocation);
            ivChatPhoto = (ImageView)itemView.findViewById(R.id.img_chat);
            ivUser = (ImageView)itemView.findViewById(R.id.ivUserChat);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            ChatModel model = getItem(position);
            if (model.getMapModel() != null){
                mClickListenerChatFirebase.clickImageMapChat(view,position,model.getMapModel().getLatitude(),model.getMapModel().getLongitude());
            }else{
                mClickListenerChatFirebase.clickImageChat(view,position,model.getUserModel().getName(),model.getUserModel().getPhoto_profile(),model.getFile().getUrl_file());
            }
        }

        public void setTxtMessage(String message){
            if (txtMessage == null)return;
            txtMessage.setText(message);
        }

        public void setIvUser(String urlPhotoUser){
            if (ivUser == null)return;
            Glide.with(ivUser.getContext()).load(urlPhotoUser).centerCrop().transform(new CircleTransform(ivUser.getContext())).override(40,40).into(ivUser);
        }

        public void setTvTimestamp(String timestamp){
            if (tvTimestamp == null)return;
            tvTimestamp.setText(converteTimestamp(timestamp));
        }

        public void setIvChatPhoto(String url){
            if (ivChatPhoto == null)return;
            Glide.with(ivChatPhoto.getContext()).load(url)
                    .override(100, 100)
                    .fitCenter()
                    .into(ivChatPhoto);
            ivChatPhoto.setOnClickListener(this);
        }

        public void tvIsLocation(int visible){
            if (tvLocation == null)return;
            tvLocation.setVisibility(visible);
        }

    }

    private CharSequence converteTimestamp(String mileSegundos){
        return DateUtils.getRelativeTimeSpanString(Long.parseLong(mileSegundos),System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
    }

如果有人能帮忙,我将非常感激:)

UserModel是null@AntonKazakov那我该怎么解决呢?可能是
public class UserModel {

    private String id;
    private String name;
    private String photo_profile;

    public UserModel() {
    }

    public UserModel(String name, String photo_profile, String id) {
        this.name = name;
        this.photo_profile = photo_profile;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoto_profile() {
        return photo_profile;
    }

    public void setPhoto_profile(String photo_profile) {
        this.photo_profile = photo_profile;
    }

    @Exclude
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}