我目前正在制作一個專案,在該專案中,用戶將垃圾分類,作為回報,它會給他們積分。使用這些積分,他們可以兌換獎勵。目前我遇到了如何在 Android Studio 的 TextView 中顯示點的問題。這是我的代碼。我的資料庫結構https://imgur.com/wOGfYOg我希望將點顯示在我的儀表板上的 TextView 上https://imgur.com/zJjSNgO 問題發生在 DocumentReference 部分。
public class MainActivity extends AppCompatActivity {
private TextView powents;
FirebaseFirestore fStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout = findViewById(R.id.logout);
ImageView qrimage = findViewById(R.id.qrimage);
powents = findViewById(R.id.powents);
fStore = FirebaseFirestore.getInstance();
DocumentReference docref = fStore.collection("Users").document("Users");
docref.addSnapshotListener(this, (documentSnapshot, error) -> {
powents.setText(Math.toIntExact(documentSnapshot.getLong("Points")));
});
try {
BarcodeEncoder barcode = new BarcodeEncoder();
Bitmap bitmap = barcode.encodeBitmap(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail(), BarcodeFormat.QR_CODE, 650, 650);
qrimage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
logout.setOnClickListener(view -> {
FirebaseAuth.getInstance().signOut();
Toast.makeText(MainActivity.this, "Logged Out!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Login.class));
finish();
});
}
}
uj5u.com熱心網友回復:
您代碼中的問題似乎出在這行代碼上:
DocumentReference docref = fStore.collection("Users").document("Users");
當您使用上述參考時,這意味著您正在嘗試讀取一個 ID 為 的檔案,該 IDUsers實際上不存在,因為您的螢屏截圖顯示檔案 ID 為[email protected]。為了解決這個問題,對上述檔案參考進行簡單更改:
DocumentReference docref = fStore.collection("Users").document("[email protected]");
只要您有適當的規則,您的代碼就應該這樣做。也不要忘記附加一個失敗的監聽器,看看是否有問題。
uj5u.com熱心網友回復:
我認為documentSnapshot.getLong("Points")為空,這就是您獲得 NPE(空指標例外)并且您無法為 textview 設定值的原因
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/441243.html
上一篇:DartFluttercom.google.firebase:firebase-messaging:23.0.0錯誤
下一篇:將帶有元組的幾列拆分為單獨的列
