您好,我的应用程序中有三个片段,其中一个是HomeFragment,我从Firebase Realtime数据库中获取所有用户详细信息,在onDataChange
事件中,我将用户数据存储到User对象中,但是当我尝试我的应用崩溃时,在按钮onClick
中使用该对象的信息。
Logcat错误
08-01 13:12:51.900 11158-11158/com.cbcwebs.luto.luto D/AndroidRuntime: Shutting down VM
08-01 13:12:51.900 11158-11158/com.cbcwebs.luto.luto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.cbcwebs.luto.luto, PID: 11158
android.content.res.Resources$NotFoundException: String resource ID #0x4d
at android.content.res.Resources.getText(Resources.java:331)
at android.widget.Toast.makeText(Toast.java:287)
at com.cbcwebs.luto.luto.HomeFragment.processUSerBuy(HomeFragment.java:82)
at com.cbcwebs.luto.luto.HomeFragment$1.onClick(HomeFragment.java:69)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
08-01 13:17:39.606 11158-11186/com.cbcwebs.luto.luto I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
[FirebaseAuth:] Preparing to create service connection to gms implementation
HomeFragment
public class HomeFragment extends Fragment {
private Integer time;
private TextView timeLeftView;
Button btn;
private Integer currentCoinsUser;
private Integer currentCoins;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
getUserInfo();
btn = (Button) rootView.findViewById(R.id.playLotteryButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
processUSerBuy();
}
});
return rootView;
}
public void processUSerBuy() {
Toast.makeText(getActivity(),currentCoinsUser, Toast.LENGTH_LONG).show();
}
public void getUserInfo() {
String UID = "XXXXXXXXXXXX";
DatabaseReference mFirebaseDbReferenceUserChild = FirebaseDatabase.getInstance().getReference("users");
DatabaseReference mFirebaseDbReferenceCurrentUser= mFirebaseDbReferenceUserChild.child(UID);
mFirebaseDbReferenceCurrentUser.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Userbalance.setText("₹"+user.getUserBalanace());
Userearning.setText("₹"+user.getUserEarning());
UserInvest.setText("₹"+user.getUserInvest());
currentCoins = user.getUserCoins();
storeUserInfo(currentCoins);
}
@Override
public void onCancelled(DatabaseError name) {
}
});
}
public void storeUserInfo(Integer currentCoins) {
currentCoinsUser = currentCoins;
}
}