问题
Im trying to add cloud database Documents into an ArrayAdapter for my card stack. I have the Collection "Users" and Document "userID" which contains the users name and gender. Im trying to add the users name to a card stack. So far Iv been able to add the users to a list but cannot get them into my cards.
When I run the app I get this error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
This error occurs at Name_On_Card.setText(strangers); in the CardAdapter.java
Ill add the code to my CardAdapter and MainActivity Im using
Model.java
public class Model {
public String name_on_card;
public ImageView card_image;
public Model(TextView name_on_card, ImageView card_image) {
}
public String getName_on_card() {
return name_on_card;
}
public void setName_on_card(String name_on_card) {
this.name_on_card = name_on_card;
}
public ImageView getCard_image() {
return card_image;
}
public void setCard_image(ImageView card_image) {
this.card_image = card_image;
}}
CardAdapter.java
public class CardAdpater extends BaseCardAdapter {
private List<Model> modelList;
private Context context;
public CardAdpater(List<Model> modelList, Context context) {
this.modelList = modelList;
this.context = context;
}
@Override
public int getCount() {
return modelList.size();
}
@Override
public int getCardLayoutId() {
return R.layout.card_item;
}
@Override
public void onBindData(int position, View cardview) {
if (modelList == null || modelList.size() == 0);
{
ImageView imageView = cardview.findViewById(R.id.card_image);
TextView textView = cardview.findViewById(R.id.name_on_card);
Model model = modelList.get(position);
textView.setText(model.getName_on_card());
imageView.setImageResource(R.drawable.ic_launcher_background);
}}}
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
TextView Name_On_Card;
FirebaseAuth mAuth;
FirebaseFirestore dataBase;
String userID;
ImageView Image_On_Card;
private SwipeCardsView swipeCardsView;
private List<Model> modelList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
dataBase = FirebaseFirestore.getInstance();
userID = mAuth.getCurrentUser().getUid();
Image_On_Card = findViewById(R.id.card_image);
Name_On_Card = findViewById(R.id.name_on_card);
//SwipeCards
swipeCardsView = findViewById(R.id.swipeCardsView);
swipeCardsView.retainLastCard(false);
swipeCardsView.enableSwipe(true);
getUserDB();
getData();
}
// Method for getting Users from database
private void getUserDB () {
dataBase.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getString("Name"));
}
Log.d(TAG, list.toString());
String strangers = list.toString();
Name_On_Card.setText(strangers);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}});
}
// CardAdpater method
private void getData() {
modelList.add(new Model(Name_On_Card,Image_On_Card));
CardAdpater cardAdpater = new CardAdpater(modelList,this);
swipeCardsView.setAdapter(cardAdpater);
}
//LogOut OnClick
public void LogOut(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
private class CardStackTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(@NonNull View page, float position) {
}}}
card_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/card_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher_round"
/>
<TextView
android:id="@+id/name_on_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingBottom="55dp"
android:text="Name Holder">
</TextView>
</RelativeLayout>
来源:https://stackoverflow.com/questions/60081740/adding-cloud-firestore-documents-into-an-arrayadapter