英文:
Variable not assigned even though I assigned it (Android Java)
问题
这是我的Java代码:
public void updateWalletString() {
User user = (User) Homeactivity.getUser();
wallet = user.getWallet();
String walletKey = getAWalletKey(wallet);
Log.v(TAG, walletKey);
}
private String getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.d(TAG, "getWalletkey: " + WalletKey[0]);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "Error retrieving walletId from database", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
但是我的Logcat输出:
D/HomeActivity: getAWalletKey: Cash Wallet(此行来自getAWalletKey()函数)
V/HomeActivity: null(此行来自updateWalletString()函数)
我已检查了代码,一切都是正确的。
英文:
This is my java code:
public void updateWalletString(){
User user = (User)Homeactivity.getUser()
wallet = user.getWallet()
String walletKey = getAWalletKey(wallet);
Log.v(TAG, walletKey);
}
private String getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.d(TAG, "getWalletkey: "+ WalletKey[0]);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "Error retrieving walletId from database", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
But my logcat printed:
<br/>
D/HomeActivity: getAWalletKey: Cash Wallet (this line comes from the getAWalletKey() function )
<br/>
V/HomeActivity: null (this line comes from the updateWalletString() function)
I checked the code, everything is correct.
答案1
得分: 0
目前在你的代码中有两个不同的线程在执行。第一个是你的主线程,它从以下代码开始执行:
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
return WalletKey[0];
还有一个异步线程从 Firebase 获取数据。在你返回WalletKey[0]
的时候,这个值仍然是null
,因为异步过程尚未完成。
我认为克服这个问题的最佳方式是修改getAWalletKey
函数以更改钱包。像这样:
public void updateWalletString(){
User user = (User) Homeactivity.getUser();
wallet = user.getWallet();
updateAWalletKey(wallet);
}
private void getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.v(TAG, walletKey);
Log.d(TAG, "getWalletkey: " + WalletKey[0]);
doTheWallet(childSnapshot.getKey()); // 不论你想对钱包做什么操作。
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "从数据库获取钱包ID出错", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
英文:
There are currently two different thread executing in your Code. The first was your main Thread that executes from
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
return WalletKey[0];
and the asynchronous thread that fetching the Data from the firebase.
By the time you returning the WalletKey[0] the value is still null
, because the asynchronous process not finished yet.
I think the best way to overcome this is to change the getAWalletKey
function to alter the wallet. Like this.
public void updateWalletString(){
User user = (User)Homeactivity.getUser()
wallet = user.getWallet()
updateAWalletKey(wallet);
}
private void getAWalletKey(Wallet w) {
String walletname = w.getName();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final String[] WalletKey = new String[1];
databaseReference.child("Users").child(uid).child("wallets").orderByChild("name")
.equalTo(walletname).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
WalletKey[0] = childSnapshot.getKey();
Log.v(TAG, walletKey);
Log.d(TAG, "getWalletkey: "+ WalletKey[0]);
doTheWallet(childSnapshot.getKey()); // whatever you want to do with the wallet.
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomeActivity.this, "Error retrieving walletId from database", Toast.LENGTH_SHORT);
}
});
return WalletKey[0];
}
专注分享java语言的经验与见解,让所有开发者获益!
评论