我试图制作一个拥有大量用户的应用。我想到的是大厅和模型,其中一个属性是用户的ArrayList。但是从我到目前为止所读到的内容来看,说起来容易做起来难。这就是我到目前为止......
Lobby.class(大堂模型)
@Parcel
public class Lobby {
private String owner;
private String location;
private double cost;
private String startDate;
private ArrayList<FirebaseUser> members;
public Lobby(){
}
public Lobby(String owner, String location, double cost, String startDate, ArrayList<FirebaseUser> members) {
this.owner = owner;
this.location = location;
this.cost = cost;
this.startDate = startDate;
this.members = members;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public ArrayList<FirebaseUser> getMembers() {
return members;
}
public void setMembers(ArrayList<FirebaseUser> members) {
this.members = members;
}
public String getDateText(){
String dateString = DateFormat.getDateInstance().format(new Date());
return dateString;
}
public int getTotalMembers(){
return members.size();
}
}
这就是我将大厅保存到firebase的地方
public class createLobby extends AppCompatActivity {
private static String TAG = "CreateLobby";
private TextView displayDate;
private DatePickerDialog.OnDateSetListener dateListener;
private EditText locIn, costIn;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private DatabaseReference usernameGet;
private Button submitLobbyButton;
private String ownerName;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_lobby);
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference();
usernameGet = databaseReference.child(user.getUid()).child("userName");
usernameGet.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ownerName = dataSnapshot.getValue(String.class);
Log.v("Username", ownerName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
locIn = (EditText)findViewById(R.id.loc_input);
costIn = (EditText)findViewById(R.id.cost_input);
displayDate = (TextView)findViewById(R.id.date_picker);
displayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
createLobby.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateListener, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
dateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month+1;
String date = month + "/" + dayOfMonth + "/" + year;
displayDate.setText(date);
}
};
submitLobbyButton = (Button)findViewById(R.id.create_lobby_button);
submitLobbyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitLobby();
}
});
}
public void submitLobby(){
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
DatabaseReference lobbyRef = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
String owenerMan = ownerName;
String gameLoc = locIn.getText().toString();
Double gameCost = Double.parseDouble(costIn.getText().toString());
String gameDate = displayDate.getText().toString();
ArrayList<FirebaseUser> lobbyMembers = new ArrayList<>();
Lobby newLobby = new Lobby(owenerMan, gameLoc, gameCost, gameDate, lobbyMembers);
lobbyMembers.add(user);
lobbyRef.push().setValue(newLobby);
Log.v("creator", owenerMan);
Log.v("newLobby", newLobby.getStartDate());
Toast.makeText(getApplicationContext(), "Lobby Added!", Toast.LENGTH_SHORT).show();
}
}
当我运行我的代码时,会出现一条错误,指出我的Lobby.class文件中的getTotalMembers()函数返回了空指针异常。数组不存储
答案 0 :(得分:1)
尝试更换此功能后,让我知道,会发生什么:
public void submitLobby(){
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
DatabaseReference lobbyRef = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
String owenerMan = ownerName;
String gameLoc = locIn.getText().toString();
Double gameCost = Double.parseDouble(costIn.getText().toString());
String gameDate = displayDate.getText().toString();
ArrayList<FirebaseUser> lobbyMembers = new ArrayList<>();
lobbyMembers.add(user); // You were adding user into Array after calling constructor
Lobby newLobby = new Lobby(owenerMan, gameLoc, gameCost, gameDate, lobbyMembers);
lobbyRef.push().setValue(newLobby);
Log.v("creator", owenerMan);
Log.v("newLobby", newLobby.getStartDate());
Toast.makeText(getApplicationContext(), "Lobby Added!", Toast.LENGTH_SHORT).show();
}