我可以获取firebase数据。但是当我尝试获取用户数据时出现了此错误。我检查了用户规则,一切正常,这是什么问题?实际上,它可以在AppCompatActivity
上使用,但是当我切换到Fragment
{
"rules": {
"Members": {
".indexOn": ["Doka", "limit"]
},
"kelessmothlar": {
".indexOn": ["smothkelesonucu"]
},
"Dokas": {
".indexOn": ["Doka", "limit"]
},
".read": true,
".write": true
}
}
错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.samin.world.BlankFragment2$1.onDataChange(BlankFragment2.java:723)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
723行是
if (!dataSnapshot.child("Members")
.child(user.getCurrentUser().getUid())
.child("smothy5")
.getValue().toString()
.equals(dataSnapshot.child("smothy5").child("Games").getValue())) {
代码Java
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment2 extends Fragment {
private FirebaseAuth user;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
String limit;
String limitsmoth2;
String limitsmoth3;
String limitsmoth4;
String limitsmoth5;
int counter1 = 0;
int countersmoth2 = 0;
int countersmoth3 = 0;
int countersmoth4 = 0;
int countersmoth5 = 0;
int counter6 = 0;
int counter7 = 0;
int counter8 = 0;
int counter9 = 0;
int counter10 = 0;
int counter11 = 0;
int counter12 = 0;
int counter13 = 0;
int counter14 = 0;
int counter15 = 0;
String old;
String oldsmoth2;
String oldsmoth3;
String oldsmoth4;
String oldsmoth5;
String old6;
String old7;
String old8;
String old9;
String old10;
String old11;
String old12;
String old13;
String old14;
String old15;
String deep;
String deepsmoth2;
String deepsmoth3;
String deepsmoth4;
String deepsmoth5;
String deep6;
String deep7;
String deep8;
String deep9;
String deep10;
String deep11;
String deep12;
String deep13;
String deep14;
String deep15;
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myview = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
final RelativeLayout smoth1 = (RelativeLayout)myview.findViewById(R.id.smothy1);
final LinearLayout smoth1liner = (LinearLayout)myview.findViewById(R.id.smothy1liner);
final LinearLayout smoth1liner2 = (LinearLayout)myview.findViewById(R.id.smothy1liner2);
final LinearLayout smoth1kose = (LinearLayout)myview.findViewById(R.id.smothy1kose);
final LinearLayout smoth1kilit = (LinearLayout)myview.findViewById(R.id.smothy1kilit);
final LinearLayout smoth1free = (LinearLayout)myview.findViewById(R.id.smothy1free);
final LinearLayout smoth1oldliner = (LinearLayout)myview.findViewById(R.id.smothy1oldlinear);
final ImageView smoth1cal = (ImageView)myview.findViewById(R.id.smothy1cal);
final ImageView smoth1simage = (ImageView)myview.findViewById(R.id.smothy1simage);
final RelativeLayout smoth2 = (RelativeLayout)myview.findViewById(R.id.smothy2);
final LinearLayout smoth2liner = (LinearLayout)myview.findViewById(R.id.smothy2liner);
final LinearLayout smoth2liner2 = (LinearLayout)myview.findViewById(R.id.smothy2liner2);
final LinearLayout smoth2kose = (LinearLayout)myview.findViewById(R.id.smothy2kose);
final LinearLayout smoth2kilit = (LinearLayout)myview.findViewById(R.id.smothy2kilit);
final LinearLayout smoth2free = (LinearLayout)myview.findViewById(R.id.smothy2free);
final LinearLayout smoth2oldliner = (LinearLayout)myview.findViewById(R.id.smothy2oldlinear);
final ImageView smoth2cal = (ImageView)myview.findViewById(R.id.smothy2cal);
final ImageView smoth2simage = (ImageView)myview.findViewById(R.id.smothy2simage);
final RelativeLayout smoth3 = (RelativeLayout)myview.findViewById(R.id.smothy3);
final LinearLayout smoth3liner = (LinearLayout)myview.findViewById(R.id.smothy3liner);
final LinearLayout smoth3liner2 = (LinearLayout)myview.findViewById(R.id.smothy3liner2);
final LinearLayout smoth3kose = (LinearLayout)myview.findViewById(R.id.smothy3kose);
final LinearLayout smoth3kilit = (LinearLayout)myview.findViewById(R.id.smothy3kilit);
final LinearLayout smoth3free = (LinearLayout)myview.findViewById(R.id.smothy3free);
final LinearLayout smoth3oldliner = (LinearLayout)myview.findViewById(R.id.smothy3oldlinear);
final ImageView smoth3cal = (ImageView)myview.findViewById(R.id.smothy3cal);
final ImageView smoth3simage = (ImageView)myview.findViewById(R.id.smothy3simage);
final RelativeLayout smoth4 = (RelativeLayout)myview.findViewById(R.id.smothy4);
final LinearLayout smoth4liner = (LinearLayout)myview.findViewById(R.id.smothy4liner);
final LinearLayout smoth4liner2 = (LinearLayout)myview.findViewById(R.id.smothy4liner2);
final LinearLayout smoth4kose = (LinearLayout)myview.findViewById(R.id.smothy4kose);
final LinearLayout smoth4kilit = (LinearLayout)myview.findViewById(R.id.smothy4kilit);
final LinearLayout smoth4free = (LinearLayout)myview.findViewById(R.id.smothy4free);
final LinearLayout smoth4oldliner = (LinearLayout)myview.findViewById(R.id.smothy4oldlinear);
final ImageView smoth4cal = (ImageView)myview.findViewById(R.id.smothy4cal);
final ImageView smoth4simage = (ImageView)myview.findViewById(R.id.smothy4simage);
final RelativeLayout smoth5 = (RelativeLayout)myview.findViewById(R.id.smothy5);
final LinearLayout smoth5liner = (LinearLayout)myview.findViewById(R.id.smothy5liner);
final LinearLayout smoth5liner2 = (LinearLayout)myview.findViewById(R.id.smothy5liner2);
final LinearLayout smoth5kose = (LinearLayout)myview.findViewById(R.id.smothy5kose);
final LinearLayout smoth5kilit = (LinearLayout)myview.findViewById(R.id.smothy5kilit);
final LinearLayout smoth5free = (LinearLayout)myview.findViewById(R.id.smothy5free);
final LinearLayout smoth5oldliner = (LinearLayout)myview.findViewById(R.id.smothy5oldlinear);
final ImageView smoth5cal = (ImageView)myview.findViewById(R.id.smothy5cal);
final ImageView smoth5simage = (ImageView)myview.findViewById(R.id.smothy5simage);
final TextView smoth1text = myview.findViewById(com.samin.world.R.id.smothy1text);
final TextView smoth2text = myview.findViewById(com.samin.world.R.id.smothy2text);
final TextView smoth3text = myview.findViewById(com.samin.world.R.id.smothy3text);
final TextView smoth4text = myview.findViewById(com.samin.world.R.id.smothy4text);
final TextView smoth5text = myview.findViewById(com.samin.world.R.id.smothy5text);
final TextView smoth1deep = myview.findViewById(com.samin.world.R.id.smothy1deep);
final TextView smoth2deep = myview.findViewById(com.samin.world.R.id.smothy2deep);
final TextView smoth3deep = myview.findViewById(com.samin.world.R.id.smothy3deep);
final TextView smoth4deep = myview.findViewById(com.samin.world.R.id.smothy4deep);
final TextView smoth5deep = myview.findViewById(com.samin.world.R.id.smothy5deep);
final TextView smoth1time = myview.findViewById(com.samin.world.R.id.smothy1time);
final TextView smoth2time = myview.findViewById(com.samin.world.R.id.smothy2time);
final TextView smoth3time = myview.findViewById(com.samin.world.R.id.smothy3time);
final TextView smoth4time = myview.findViewById(com.samin.world.R.id.smothy4time);
final TextView smoth5time = myview.findViewById(com.samin.world.R.id.smothy5time);
final TextView smoth1old = myview.findViewById(com.samin.world.R.id.smothy1old);
final TextView smoth2old = myview.findViewById(com.samin.world.R.id.smothy2old);
final TextView smoth3old = myview.findViewById(com.samin.world.R.id.smothy3old);
final TextView smoth4old = myview.findViewById(com.samin.world.R.id.smothy4old);
final TextView smoth5old = myview.findViewById(com.samin.world.R.id.smothy5old);
try {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
String langs = Locale.getDefault().getLanguage();
String Gamessmoth4 = dataSnapshot.child("smothy4").child("Games").getValue().toString();
String timesmoth4 = dataSnapshot.child("smothy4").child("Newtime").getValue().toString();
if (langs.equals("tr")) {
oldsmoth4 = dataSnapshot.child("smothy4").child("Prediction").getValue().toString();
} else {
oldsmoth4 = dataSnapshot.child("smothy4").child("old").getValue().toString();
}
deepsmoth4 = dataSnapshot.child("smothy4").child("deep").getValue().toString();
limitsmoth4 = dataSnapshot.child("smothy4").child("limit").getValue().toString();
if (Gamessmoth4.equals(" ")) {
//Yazilar
smoth4text.setVisibility(View.GONE);
smoth4deep.setVisibility(View.GONE);
smoth4time.setVisibility(View.GONE);
smoth4old.setVisibility(View.GONE);
smoth4text.setText(Gamessmoth4);
smoth4.setVisibility(View.GONE);
//Linerlar
smoth4liner.setVisibility(View.GONE);
smoth4liner2.setVisibility(View.GONE);
smoth4kose.setVisibility(View.GONE);
smoth4kilit.setVisibility(View.GONE);
smoth4free.setVisibility(View.GONE);
smoth4oldliner.setVisibility(View.GONE);
//Imageler
smoth4cal.setVisibility(View.GONE);
smoth4simage.setVisibility(View.GONE);
} else {
//Yazilar
smoth4text.setVisibility(View.VISIBLE);
smoth4deep.setVisibility(View.INVISIBLE);
smoth4time.setVisibility(View.VISIBLE);
smoth4old.setVisibility(View.VISIBLE);
smoth4.setVisibility(View.VISIBLE);
//Linerlar
smoth4liner.setVisibility(View.VISIBLE);
smoth4liner2.setVisibility(View.VISIBLE);
smoth4kose.setVisibility(View.VISIBLE);
smoth4kilit.setVisibility(View.VISIBLE);
smoth4free.setVisibility(View.VISIBLE);
smoth4oldliner.setVisibility(View.VISIBLE);
//Imageler
smoth4cal.setVisibility(View.VISIBLE);
smoth4simage.setVisibility(View.VISIBLE);
if (!dataSnapshot.child("Members").child(user.getCurrentUser().getUid()).child("smothy4").getValue().toString().equals(dataSnapshot.child("smothy4").child("Games").getValue())) {
if (countersmoth4 == 0) {
smoth4old.setText(getResources().getString(R.string.credit) + " " + limitsmoth4);
countersmoth4++;
}
//patopat1.setText(peli1);
smoth4time.setText(timesmoth4);
if (countersmoth4 == 1) {
//smoth4deep.setText(getResources().getString(R.string.show));
smoth4deep.setVisibility(View.INVISIBLE);
countersmoth4++;
}
} else {
//patopat1.setText(peli1);
smoth4time.setText(timesmoth4);
smoth4old.setText(oldsmoth4);
smoth4deep.setText(deepsmoth4);
smoth4deep.setVisibility(View.VISIBLE);
smoth4free.setVisibility(View.INVISIBLE);
Log.d("ERR", "BURASI ÇALIŞTI");
}
}
String Gamessmoth5 = dataSnapshot.child("smothy5").child("Games").getValue().toString();
String timesmoth5 = dataSnapshot.child("smothy5").child("Newtime").getValue().toString();
if (langs.equals("tr")) {
oldsmoth5 = dataSnapshot.child("smothy5").child("Prediction").getValue().toString();
} else {
oldsmoth5 = dataSnapshot.child("smothy5").child("old").getValue().toString();
}
deepsmoth5 = dataSnapshot.child("smothy5").child("deep").getValue().toString();
limitsmoth5 = dataSnapshot.child("smothy5").child("limit").getValue().toString();
if (Gamessmoth5.equals(" ")) {
//Yazilar
smoth5text.setVisibility(View.GONE);
smoth5deep.setVisibility(View.GONE);
smoth5time.setVisibility(View.GONE);
smoth5old.setVisibility(View.GONE);
smoth5text.setText(Gamessmoth5);
smoth5.setVisibility(View.GONE);
//Linerlar
smoth5liner.setVisibility(View.GONE);
smoth5liner2.setVisibility(View.GONE);
smoth5kose.setVisibility(View.GONE);
smoth5kilit.setVisibility(View.GONE);
smoth5free.setVisibility(View.GONE);
smoth5oldliner.setVisibility(View.GONE);
//Imageler
smoth5cal.setVisibility(View.GONE);
smoth5simage.setVisibility(View.GONE);
} else {
//Yazilar
smoth5text.setVisibility(View.VISIBLE);
smoth5deep.setVisibility(View.INVISIBLE);
smoth5time.setVisibility(View.VISIBLE);
smoth5old.setVisibility(View.VISIBLE);
smoth5.setVisibility(View.VISIBLE);
//Linerlar
smoth5liner.setVisibility(View.VISIBLE);
smoth5liner2.setVisibility(View.VISIBLE);
smoth5kose.setVisibility(View.VISIBLE);
smoth5kilit.setVisibility(View.VISIBLE);
smoth5free.setVisibility(View.VISIBLE);
smoth5oldliner.setVisibility(View.VISIBLE);
//Imageler
smoth5cal.setVisibility(View.VISIBLE);
smoth5simage.setVisibility(View.VISIBLE);
if (!dataSnapshot.child("Members").child(user.getCurrentUser().getUid()).child("smothy5").getValue().toString().equals(dataSnapshot.child("smothy5").child("Games").getValue())) {
if (countersmoth5 == 0) {
smoth5old.setText(getResources().getString(R.string.credit) + " " + limitsmoth5);
countersmoth5++;
}
//patopat1.setText(peli1);
smoth5time.setText(timesmoth5);
if (countersmoth5 == 1) {
//smoth5deep.setText(getResources().getString(R.string.show));
smoth5deep.setVisibility(View.INVISIBLE);
countersmoth5++;
}
} else {
//patopat1.setText(peli1);
smoth5time.setText(timesmoth5);
smoth5old.setText(oldsmoth5);
smoth5deep.setText(deepsmoth5);
smoth5deep.setVisibility(View.VISIBLE);
smoth5free.setVisibility(View.INVISIBLE);
Log.d("ERR", "BURASI ÇALIŞTI");
}
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
} catch (Exception e) {
//Toast.makeText(this.getActivity(), R.string.errenoughtcredit, Toast.LENGTH_LONG).show();
}
return myview;
}
}
答案 0 :(得分:2)
我在您的代码中看到的是
用户为空
查看
尝试一下
"816 ".length === 5
代替
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
然后在第723行使用此
private FirebaseAuth user;
希望它会起作用