当我尝试访问已在以下类中初始化的列表锦标赛时:
public class AdminControl extends AppCompatActivity {
Firebase ref;
FloatingActionButton fab;
public static List <Tournament> tournaments;
ListView list;
Tournament [] temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
AdminLogin.firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tournaments = (ArrayList) dataSnapshot.getValue();
Object [] objArr = tournaments.toArray();
temp = new Tournament[objArr.length];
for (int i = 0; i < objArr.length; i++){
temp[i] = (Tournament) objArr[i];
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
if (tournaments == null){
tournaments = new ArrayList<Tournament>();
}
list = (ListView) findViewById(R.id.listView);
ArrayAdapter<Tournament> tempAdapter = new ArrayAdapter<Tournament>(
this, android.R.layout.simple_list_item_1, temp);
list.setAdapter(tempAdapter);
}
public void createNewTournament(View v) {
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}
在我离开该任务并通过调用上面的TournamentCreator
方法createNewTournament
实例化新活动后,我尝试从static
访问TournamentCreator
列表锦标赛活动。但是,它显示为null
(Object a
)。我认为静态变量总是保持不变。在我正在做的事情中是否存在某种问题?
比赛创作者类
public class TournamentCreator extends AppCompatActivity {
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
Object a = AdminControl.tournaments;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament_creator);
b = (Button) findViewById(R.id.button3);
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
beginTournament(v);
}
});
}
public void beginTournament(View v) {
EditText text = (EditText) findViewById(R.id.editText);
Tournament newTourn = new Tournament(text.getText().toString());
AdminControl.tournaments.add(newTourn);
Firebase userRef = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
userRef.setValue(AdminControl.tournaments);
finish();
}
}