我创建了Firebase登录和注册应用程序!我希望在登录到应用程序后,用户可以从Firebase中检索值。
我想在这里显示点数:After Login User can see this page
我使用了此代码但登录到应用程序数据后,它没有返回它。 资料活动:
package com.helploger.www.ezzeearn;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Map;
public class ProfileActivity extends AppCompatActivity implements View.OnClickListener {
Toolbar toolbar;
//firebase auth object
private FirebaseAuth firebaseAuth;
//firebase database
private TextView tv;
//view objects
private TextView textViewUserEmail;
private Button buttonLogout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
//Toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//initializing firebase authentication object
firebaseAuth = FirebaseAuth.getInstance();
//if the user is not logged in
//that means current user will return null
if (firebaseAuth.getCurrentUser() == null) {
//closing this activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
//getting current user
FirebaseUser user = firebaseAuth.getCurrentUser();
//initializing views
textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
buttonLogout = (Button) findViewById(R.id.buttonLogout);
tv = (TextView) findViewById(R.id.tv);
//displaying logged in user name
textViewUserEmail.setText("Welcome " + user.getEmail());
//adding listener to button
buttonLogout.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ezzeearnRef = rootRef.child(firebaseAuth.getCurrentUser().getUid());
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String points = dataSnapshot.child("points").getValue(String.class);
tv.setText(points);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
ezzeearnRef.addListenerForSingleValueEvent(eventListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout) {
firebaseAuth.signOut();
finish();
startActivity(new Intent(this, LoginActivity.class));
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
//if logout is pressed
if(view == buttonLogout){
//logging out the user
firebaseAuth.signOut();
//closing activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
}
}
答案 0 :(得分:6)
我可以很快看到两个错误:
"points"
,但JSON中有"Points"
。大小写很重要,因此您当前的代码会读取不存在的节点。所以我认为你会想要:
public void onDataChange(DataSnapshot dataSnapshot) {
long points = dataSnapshot.child("Points").getValue(Long.class);
tv.setText(String.valueOf(points));
}
请注意,如果您通过调试器执行代码,则这些类型的问题最容易进行故障排除。在这种情况下,在onDataChange
中放置一个断点应该表明它会被触发,这样可以更容易地发现拼写错误。