用户注销时,以下活动崩溃。它说FirebaseAuth为空。我没有得到用户ID。我使用的是firebase 10.0.1版本。
public class CardViewActivity extends AppCompatActivity {
RecyclerView mContextList;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabase;
private Query mQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_view);
mAuth=FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser()==null){
Intent loginIntent=new Intent(CardViewActivity.this,LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}else {
startService(new Intent(CardViewActivity.this, Clipboard.class));
}
}
};
mDatabase= FirebaseDatabase.getInstance().getReference().child("Pins");
mQuery=mDatabase.orderByChild("uid").equalTo(mAuth.getCurrentUser().getUid());
mContextList=(RecyclerView)findViewById(R.id.context_list);
mContextList.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mContextList.setLayoutManager(layoutManager);
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<Context,ContextViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Context, ContextViewHolder>(
Context.class,
R.layout.context_row,
ContextViewHolder.class,
mQuery
) {
@Override
protected void populateViewHolder(ContextViewHolder viewHolder, final Context model, int position) {
final String key=getRef(position).getKey();
viewHolder.setDate(model.getDate());
viewHolder.setText(model.getText());
viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CardViewActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Delete..");
// Setting Dialog Message
alertDialog.setMessage("Do you want to delete the selected context?");
// Setting Icon to Dialog
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
mDatabase.child(key).removeValue();
Toast.makeText(CardViewActivity.this,"Deleted!",Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
}
});
// Showing Alert Message
alertDialog.show();
return true;
}
});
}
};
mContextList.setAdapter(firebaseRecyclerAdapter);
}
public static class ContextViewHolder extends RecyclerView.ViewHolder{
View mView;
public ContextViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setDate(String date){
TextView dateTextView=(TextView)mView.findViewById(R.id.context_date);
dateTextView.setText(date);
}
public void setText(String text){
TextView textTextView=(TextView)mView.findViewById(R.id.context_text);
textTextView.setText(text);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.action_logout){
logout();
}
return super.onOptionsItemSelected(item);
}
private void logout() {
stopService(new Intent(this, Clipboard.class));
mAuth.signOut();
}
}
我收到以下错误
07-11 05:37:00.990 3302-3302/com.example.amonc.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.amonc.myapplication, PID: 3302
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.amonc.myapplication/com.example.amonc.myapplication.CardViewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.amonc.myapplication.CardViewActivity.onCreate(CardViewActivity.java:62)
at android.app.Activity.performCreate(Activity.java:6384)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
在onCreate()
中运行此行时,您的用户尚未加载:
mQuery=mDatabase.orderByChild("uid").equalTo(mAuth.getCurrentUser().getUid());
要确保加载用户,请将需要用户的代码移到onAuthStateChanged()
方法中:
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser()==null){
Intent loginIntent=new Intent(CardViewActivity.this,LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}else {
startService(new Intent(CardViewActivity.this, Clipboard.class));
mQuery=mDatabase.orderByChild("uid").equalTo(mAuth.getCurrentUser().getUid());
}
}
};
但请阅读评论中提供的Ken链接,因为NullPointerException
很容易调试。只需运行附带调试器的应用程序,Android Studio就可以解决问题的根源。该链接解释了如何从那里继续。