1

我做了一个应用程序,可以让用户登录并在谷歌地图上设置标记,并将标记连接到属于设置它的用户的聊天室。无论我如何尝试,Firebase用户标识为空?

现在的问题是,我不能在Chatactivity和MapFragment得到的uid无论我怎样努力,也不断出现空,我也不是什么问题 下面是三个活动:

可有人请帮帮我~~

LoginActivity

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    auth = FirebaseAuth.getInstance(); 

    if (auth.getCurrentUser() != null) { 
     startActivity(new Intent(LoginActivity.this, MainActivity.class)); 
     finish(); 
    } 
    //authenticate user 
    auth.signInWithEmailAndPassword(email, password) 
      .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        // If sign in fails, display a message to the user. If sign in succeeds 
        // the auth state listener will be notified and logic to handle the 
        // signed in user can be handled in the listener. 
        if (!task.isSuccessful()) { 
         // there was an error 
         if (password.length() < 6) { 
          edUserid.setError("密碼太短,請輸入超過6個字元!"); 
         } else { 
          Toast.makeText(LoginActivity.this, "登入失敗", Toast.LENGTH_LONG).show(); 
         } 
        } else { 
         Toast.makeText(getApplicationContext(),"登入成功",Toast.LENGTH_LONG).show(); 
         startActivity(new Intent(LoginActivity.this, MainActivity.class)); 

         finish(); 
        } 
       } 
      }); 
} 

ChatActivity

fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      EditText input = (EditText)findViewById(R.id.input); 

      // Read the input field and push a new instance 
      // of ChatMessage to the Firebase database 
      FirebaseDatabase.getInstance() 
        .getReference(CHAT_PATH) 
        .push() 
        .setValue(new ChatMessage(input.getText().toString(), 
          FirebaseAuth.getInstance() 
            .getCurrentUser() 
            .getDisplayName()) 
        ); 

      // Clear the input 
      input.setText(""); 
     } 
    }); 
    ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages); 

    adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class, 
      R.layout.message, FirebaseDatabase.getInstance().getReference(CHAT_PATH)) { 
     @Override 
     protected void populateView(View v, ChatMessage model, int position) { 
      // Get references to the views of message.xml 
      TextView messageText = (TextView)v.findViewById(R.id.message_text); 
      TextView messageUser = (TextView)v.findViewById(R.id.message_user); 
      TextView messageTime = (TextView)v.findViewById(R.id.message_time); 

      // Set their text 
      messageText.setText(model.getMessageText()); 
      messageUser.setText(model.getMessageUser()); 

      // Format the date before showing it 
      messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", 
        model.getMessageTime())); 
     } 
    }; 

    listOfMessages.setAdapter(adapter); 

} 

} 

MapFragment(的一部分)

 mFirebaseDatabase = FirebaseDatabase.getInstance(); 


     mFirebaseRef = mFirebaseDatabase.getReference("Map/"+userUID); 
     mFirebaseRef.addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 

       LatLng myLatLon = dataSnapshot.getValue(FirebaseMarker.class).toLatLng(); 

       // stash the key in the title, for recall later 

       Marker myMarker = mgoogleMap.addMarker(new MarkerOptions() 
         .position(myLatLon).draggable(true).icon(BitmapDescriptorFactory.fromResource(R.drawable.seedloc2)).title(dataSnapshot.getKey())); 

       // cache the marker locally 
       markers.put(dataSnapshot.getKey(), myMarker); 
      } 

      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
       LatLng myLatLon = dataSnapshot.getValue(FirebaseMarker.class).toLatLng(); 

       // Move markers on the map if changed on Firebase 
       Marker changedMarker = markers.get(dataSnapshot.getKey()); 
       changedMarker.setPosition(myLatLon); 

      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 

      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 
       Marker deadMarker = markers.get(dataSnapshot.getKey()); 
       deadMarker.remove(); 
       markers.remove(dataSnapshot.getKey()); 
       Log.v(TAG, "moved !" + dataSnapshot.getValue()); 


      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       Log.v(TAG, "canceled!" + databaseError.getMessage()); 

      } 
     }); 

    } 
} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    MapsInitializer.initialize(getContext()); 

    mgoogleMap = googleMap; 
    googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener(){ 

     @Override 
     public void onInfoWindowClick(Marker marker) { 
      Intent intent = new Intent(getActivity(),ChatActivity.class); 
      startActivity(intent); 



     } 
    }); 
+0

您是否在任何阶段使用Firebase身份验证? –

+0

是的,我使用电子邮件验证,我有一个可以创建帐户的活动,并且它成功获取用户ID,但其他活动不能。 –

回答

1

如果在这一活动中,你可以正确得到uid,这是你说的,然后保存uid的一个值变量并将其存储到intent并在活动之间使用它。在每个活动中都没有必要获得uid。你只需要得到它一次。

//FirstActivity 
Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 
intent.putExtra("uid", uid); 
startActivity(intent); 

并获得UID回到第二个活动,请使用此代码:如果你需要uid在您的整个应用程序,然后使用SharedPreferences来存储它

//SecondActivity 
String uid = (String) getIntent().getExtras().get("uid"); 

希望它有帮助。

+0

我明白了,让我试试看,非常感谢。 –

相关问题