我正在使用Google FireBase FireStore数据库创建一个简单的应用程序,但我遇到了一些问题。我正在创建一个对象的HashMap,然后将HashMap传递给第二个活动,但这导致我的应用程序崩溃。
这是我的对象类,称为“ Oddil”:
public class Oddil {
private String oddilCislo;
private String oddilJmeno;
private String oddilBarva;
public Oddil(String oddilCislo, String oddilJmeno, String oddilBarva) {
this.oddilCislo = oddilCislo;
this.oddilJmeno = oddilJmeno;
this.oddilBarva = oddilBarva;
}
这是我的MainActivity.java文件:
public class MainActivity extends AppCompatActivity {
FirebaseFirestore db = FirebaseFirestore.getInstance();
HashMap<String, Oddil> oddilHashMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView laJmeno = (TextView) findViewById(R.id.laJmeno);
laJmeno.setText(getIntent().getStringExtra("jmeno"));
final ImageButton tlDeti = (ImageButton) findViewById(R.id.tlDeti);
ImageButton tlUklid = (ImageButton) findViewById(R.id.tlUklid);
ImageButton tlOlymp = (ImageButton) findViewById(R.id.tlOlymp);
tlDeti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //tlDetiOnClickListener
tlDeti.setClickable(false);
oddilHashMap.clear();
Animation scale = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
v.startAnimation(scale);
db.collection("dzarda").document("testtabor").collection("oddily")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String oddilCislo = document.getId();
String oddilJmeno = document.getString("jmeno");
String oddilBarva = document.getString("barva");
oddilHashMap.put(oddilCislo, new Oddil(oddilCislo, oddilJmeno, oddilBarva));
}
Intent intent = new Intent(MainActivity.this, DetiActivity.class);
intent.putExtra("jmeno",getIntent().getStringExtra("jmeno"));
intent.putExtra("oddily", oddilHashMap);
startActivity(intent);
overridePendingTransition(R.anim.fade_in_start, R.anim.fade_out_start);
tlDeti.setClickable(true);
} else {
tlDeti.setClickable(true);
}
}
});
}
});
问题似乎出在这行
intent.putExtra("oddily", oddilHashMap);
当我在该行的前面放置两个正斜杠时,该应用程序不会崩溃。
有人有一个主意,为什么我不能将HashMap传递给我的第二个活动?谢谢您的所有回答,我非常感谢。