我正在按照a tutorial向我的应用添加消息传递功能,但是出现“未知来源”错误,我不确定为什么。
我做的唯一与视频不同的事情是使用了哈希映射,即使我完全按照显示的方式复制代码,错误仍然存在。
public class MessageActivity extends AppCompatActivity {
private RecyclerView messagesRecycleView;
private String userProfileName;
private String userProfilePic;
private String timeStamp;
private String messageTimeStamp;
private HashMap<String, Object> messageDictionary;
private FirebaseUser currentFirebaseUser;
private TextView userNameTextView;
private CircleImageView userProfilePicture;
private EditText messageInputEditText;
private DatabaseReference firebaseDatabase;
private DatabaseReference messesagesRef;
private DatabaseReference RootRef;
private String id;
private String messageSender, messageReceiver;
private Message messages;
private ArrayList<Message> messageList;
private MessageAdapter messageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
initRecycleView();
linkUpViews();
createObjects();
setUpFirebase();
retreiveBundle();
messageSender = currentFirebaseUser.getUid();
messageReceiver = id;
}
//find views by id
public void linkUpViews() {
userNameTextView = findViewById(R.id.userNameTv);
userProfilePicture = findViewById(R.id.chatProfilePic);
messageInputEditText = findViewById(R.id.chatboxEditText);
}
//set up RecycleView
public void initRecycleView() {
messagesRecycleView = findViewById(R.id.messagesRecycleView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
messagesRecycleView.setLayoutManager(linearLayoutManager);
messageAdapter = new MessageAdapter(MessageActivity.this, messageList);
messagesRecycleView.setAdapter(messageAdapter);
}
//create objects
public void createObjects() {
messageDictionary = new HashMap<>();
//messageList = new ArrayList<>();
}
public void sendMessageButton(View view) {
sendMessages();
//saveMessages();
}
//retrieves selected user's name and profile pic from message activity and updates profile image and name
public void retreiveBundle() {
Bundle bundle = getIntent().getExtras();
userProfileName = bundle.getString("profileName");
userProfilePic = bundle.getString("profilePic");
timeStamp = bundle.getString("timestamp");
id = bundle.getString("id");
Log.i("timestamp", timeStamp);
userNameTextView.setText(userProfileName);
Picasso.get().load(userProfilePic).into(userProfilePicture);
}
//goes back to previous activity
public void backToShop(View view) {
Intent intent = new Intent(MessageActivity.this, MainActivity.class);
startActivity(intent);
}
//Set up Firebase connection
public void setUpFirebase() {
messesagesRef = FirebaseDatabase.getInstance().getReference();
RootRef = FirebaseDatabase.getInstance().getReference();
firebaseDatabase = FirebaseDatabase.getInstance().getReference();
currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
}
//Store messages to Firebase
public void sendMessages() {
if (TextUtils.isEmpty(messageInputEditText.getText().toString())) {
Toast.makeText(this, "Please enter a message", Toast.LENGTH_SHORT).show();
} else {
String messageSenderRef = "Messages/" + messageSender + "/" + messageReceiver;
String messageReceiverRef = "Messages/" + messageReceiver + "/" + messageSender;
messesagesRef.child("Messages").child(messageSender).child(messageReceiver).push();
String messagePushKey = messesagesRef.getKey();
messageDictionary.put("message", messageInputEditText.getText().toString());
messageDictionary.put("sender", messageSender);
messageDictionary.put("receiver", messageReceiver);
HashMap<String, Object> messageBodyDetails = new HashMap<>();
messageBodyDetails.put(messageSenderRef + "/" + messagePushKey, messageBodyDetails);
messageBodyDetails.put(messageReceiverRef + "/" + messagePushKey, messageBodyDetails);
RootRef.updateChildren(messageBodyDetails).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MessageActivity.this, "Sent message...", Toast.LENGTH_SHORT).show();
} else {
Log.e("Error", task.getException().getMessage().toString());
}
}
});
}
}
}
我收到一堆未知的源错误:
Caused by: java.lang.StackOverflowError: stack size 8MB
at java.util.HashMap$EntryIterator.<init>(HashMap.java:889)
at java.util.HashMap$EntryIterator.<init>(HashMap.java)
at java.util.HashMap.newEntryIterator(HashMap.java:1160)
at java.util.HashMap$EntrySet.iterator(HashMap.java:1309)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
2019-09-28 10:14:32.887 5108-5108/com.examp.smartshop E/AndroidRuntime: at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)
at com.google.android.gms.internal.zzekh.zzbw(Unknown Source)