我是编程新手,这是我第一次使用Google Firebase。 我的主要活动似乎工作正常。我可以将应用上的聊天室添加到Firebase。
当我进入其中一个聊天室并发送消息时,应用程序崩溃了。没有消息添加到Firebase。如果你们中的任何人花时间阅读代码并找到解决方案,我将不胜感激。
主要活动:
public class MainActivity extends AppCompatActivity {
private Button add_room;
private EditText room_name;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String>list_of_rooms = new ArrayList<>();
private String name;
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_room = (Button) findViewById(R.id.btn_add_room);
room_name = (EditText) findViewById(R.id.room_name_editText);
listView = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_rooms);
listView.setAdapter(arrayAdapter);
request_user_name();
add_room.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(room_name.getText().toString(), "");
root.updateChildren(map);
}
});
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()) {
set.add(((DataSnapshot) i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("room_name",((TextView)view).getText().toString() );
intent.putExtra("user_name",name);
startActivity(intent);
}
});
}
private void request_user_name() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter name");
final EditText input_field = new EditText(this);
builder.setView(input_field);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
name = input_field.getText().toString();
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
request_user_name();
}
});
builder.show();}}
聊天室课程:
public class Chat_Room extends AppCompatActivity{
private Button btn_send_msg;
private TextView chat_conversation;
private EditText input_msg;
private String user_name;
private String room_name;
private DatabaseReference root;
private String temp_key;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_room);
btn_send_msg = (Button) findViewById(R.id.btn_send);
input_msg = (EditText) findViewById(R.id.msg_txt) ;
chat_conversation =(TextView) findViewById(R.id.textView3);
user_name = getIntent().getExtras().get("user_name").toString();
room_name = getIntent().getExtras().get("room_name").toString();
setTitle(" Room - "+room_name);
root = FirebaseDatabase.getInstance().getReference().child(room_name);
btn_send_msg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String,Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
root.updateChildren(map);
DatabaseReference message_root = root.child(temp_key);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("name",user_name);
map2.put("msg",input_msg);
message_root.updateChildren(map2);
root.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
private String chat_msg;
private String chat_username;
private void append_chat_conversation(DataSnapshot datasnapshot)
{
Iterator i = datasnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String)((DataSnapshot)i.next()).getValue();
chat_username = (String)((DataSnapshot)i.next()).getValue();
chat_conversation.append(chat_username +" : "+chat_msg+" \n");
}
}}
以下是logcat的错误:
---------崩溃的开始
04-03 12:12:32.557 2493-2493/com.example.jens.rtchat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jens.rtchat, PID: 2493
com.google.firebase.database.DatabaseException: Failed to parse node with class class android.support.v7.widget.AppCompatEditText
at com.google.android.gms.internal.zzahv.zza(Unknown Source)
at com.google.android.gms.internal.zzahv.zzaq(Unknown Source)
at com.google.android.gms.internal.zzaiw.zzc(Unknown Source)
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.updateChildren(Unknown Source)
at com.example.jens.rtchat.Chat_Room$1.onClick(Chat_Room.java:69)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)