gpt4 book ai didi

java - 调用另一个方法后字符串值null android java

转载 作者:行者123 更新时间:2023-12-02 01:10:19 26 4
gpt4 key购买 nike

私有(private)变量audioLink在调用setAudio()等另一个方法后为null,即使它在onComplete方法之外也是null。我厌倦了解决这个问题。

private String audioLink;

private void setData() {
//FireBase Get Details Section
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("SUBJECT")
.document(subjectName).collection("CHAPTER")
.document(chapterName.toUpperCase())
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
txtDetails.setText(document.get("chapterDetails").toString());
audioLink = document.get("audioLink").toString();//Assigning Value
} else {
txtDetails.setText("Empty");
}
} else {
Log.d("Error", "Cached get failed: ", task.getException());
}
}
});
}

private void setAudio() {
try {
String url = audioLink; // your URL here
} catch (Exception e) {
}
}

最佳答案

您需要在 onComplete 方法中调用它:

private String audioLink;

private void setData() {
//FireBase Get Details Section
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("SUBJECT")
.document(subjectName).collection("CHAPTER")
.document(chapterName.toUpperCase())
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
txtDetails.setText(document.get("chapterDetails").toString());
audioLink = document.get("audioLink").toString();//Assigning Value
setAudio(); // you need to call if from here
} else {
txtDetails.setText("Empty");
}
} else {
Log.d("Error", "Cached get failed: ", task.getException());
}
}
});
}

关于java - 调用另一个方法后字符串值null android java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59496041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com