Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

android - Can't get data from firebase using custom adapter, "Сan't pass null for argument pathString in child ()"

I don't quite understand how the custom arrayadapter works yet, but one of them already works in my project, so I decided to add it to other lists as well. I'm not sure, but I think the problem is in the re-views from addFriends to Message

private void getFriends(){
    DatabaseReference isRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Friends");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if(listTemp.size() > 0){listTemp.clear();}
            for (DataSnapshot ds : snapshot.getChildren()){
                addFriends addFriend = ds.getValue(addFriends.class);
                if(addFriend != null){  //переоброзование в вид с чатом
                    String message = "put message is here, don`t forget. @chatID";
                    String nameuser = addFriend.username1;
                    String userID = addFriend.uid2;
                    Message mainFriends = new Message(message, nameuser, userID);
                    if(mainFriends != null) {
                        listTemp.add(mainFriends);
                    }
                }
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    };
    isRef.addValueEventListener(valueEventListener);
}

The problem is pointed exactly to private void getFriends(){ problem:

Unable to start activity ComponentInfo{esport.inyourlife.myproektlink.Profile.Friends}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() android.app.ActivityThread.performLaunchActivity

and also the code of my adapter which I borrowed:

package #.Consts;

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.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;

import esport.inyourlife.insport.R;

public class CustomAdapter extends ArrayAdapter<Message> implements View.OnClickListener{
    private ArrayList<Message> dataSet;
    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        TextView txtName;
        TextView txtType;
        ImageView info;
    }

    public CustomAdapter(ArrayList<Message> data, Context context) {
        super(context, R.layout.msg_center_item, data);
        this.dataSet = data;
        this.mContext=context;

    }

    @Override
    public void onClick(View v) {

        int position=(Integer) v.getTag();
        Object object= getItem(position);
        Message dataModel=(Message) object;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Message dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.msg_center_item, parent, false);
            viewHolder.txtName = convertView.findViewById(R.id.tvDescr);
            viewHolder.txtType = convertView.findViewById(R.id.tvPrice);
            viewHolder.info = convertView.findViewById(R.id.uImgg);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txtName.setText(dataModel.nameuser);
        viewHolder.txtType.setText(dataModel.message);
        // Return the completed view to render on screen
        return convertView;
    }
}

item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal">
    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewsdd"
        android:layout_width="86dp"
        android:layout_height="83dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:cardBackgroundColor="@color/common_google_signin_btn_text_dark_disabled"
        app:cardCornerRadius="50dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/uImgg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:src="@mipmap/ic_launcher_round" />
    </androidx.cardview.widget.CardView>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tvDescr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text=""
            android:textSize="20sp">
        </TextView>
        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="18dp"
            android:text="">
        </TextView>
    </LinearLayout>
</LinearLayout>

if all the Friends.class code comes in handy

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

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

import esport.inyourlife.insport.Consts.CustomAdapter;
import esport.inyourlife.insport.Consts.CustomFriends;
import esport.inyourlife.insport.Consts.Message;
import esport.inyourlife.insport.Consts.addFriends;
import esport.inyourlife.insport.R;
import esport.inyourlife.insport.findtest;

public class Friends extends AppCompatActivity {
    private final FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private ArrayAdapter adapter;
    private ArrayList<Message> listTemp;
    private String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends);
        setTitle("Друзья");
        assert getSupportActionBar() != null;   //null check
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getFriends();
        TabLayout tabs = findViewById(R.id.tabsFriends);
        LinearLayout freq = findViewById(R.id.support_layout);
        ListView friendList = findViewById(R.id.FriendList);
        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    freq.setVisibility(View.GONE);
                    friendList.setVisibility(View.VISIBLE);
                    getFriends();
                }
                 else if (position == 1) {
                    freq.setVisibility(View.VISIBLE);
                    friendList.setVisibility(View.GONE);
                    getReqs();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        ImageButton add = findViewById(R.id.addFriendToAll);
        add.setOnClickListener(v -> {
            startActivity(new Intent(this, findtest.class));
        });
        Button reqs = findViewById(R.id.reqs);
        reqs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Friends.this, friendsRequest.class));
            }
        });
        listTemp = new ArrayList<>();
        adapter = new CustomAdapter(listTemp, getApplicationContext());
        friendList.setAdapter(adapter);
        FirebaseUser user = mAuth.getCurrentUser();
        if (user == null) {this.finish();}
        if (user != null){
        uid = user.getUid();
        }
    }

    private void getFriends(){//получение друзей в 05/02 было из users/uid/friends
        DatabaseReference isRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Friends");
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(listTemp.size() > 0){listTemp.clear();}
                for (DataSnapshot ds : snapshot.getChildren()){
                    addFriends addFriend = ds.getValue(addFriends.class);
                    if(addFriend != null){  //переоброзование в вид с чатом
                        String message = "put message is here, don`t forget. @chatID";
                        String nameuser = addFriend.username1;
                        String userID = addFriend.uid2;
                        Message mainFriends = new Message(message, nameuser, userID);
                        if(mainFriends != null) {
                            listTemp.add(mainFriends);
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        };
        isRef.addValueEventListener(valueEventListener);
    }

    private void getReqs() {
        /*DatabaseReference isRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Friends-request");
        ValueEventListener valueEventListener 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It says in your error

Can't pass null for argument 'pathString' in child()

Which is exactly like it says. It is probably that your uid is equal to null. Check that variable before sending it. I can see in your code that you are calling getFriends() before you assign value to uid, which is causing this error.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);
    setTitle("Друзья");
    assert getSupportActionBar() != null;   //null check
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    FirebaseUser user = mAuth.getCurrentUser();
    if (user == null) this.finish();
    if (user != null){
      uid = user.getUid();
    }

    getFriends();

Just get the uid before calling getFriends()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...