2017-09-17 121 views
0

我是Firebase的新手。我試圖保存從Android發送到實時數據庫的POJO。com.google.firebase.database.DatabaseException:不支持字符,請字符串

我有這樣的POJO:

@IgnoreExtraProperties 
public class Member implements Parcelable{ 

public String name; 

public Member() {} 

public Member(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

/**=================== */ 
/** Parcelable section */ 
/**=================== */ 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public Member createFromParcel(Parcel in) { 
     return new Member(in); 
    } 

    public Member[] newArray(int size) { 
     return new Member[size]; 
    } 
}; 

public Member(Parcel in){ 
    this.name = in.readString(); 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(this.name); 
} 
} 

和活動是這樣的:

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example, PID: 16332 
com.google.firebase.database.DatabaseException: Characters are not supported, please strings 
    at com.google.android.gms.internal.zg.zzao(Unknown Source) 
    at com.google.android.gms.internal.zg.zzar(Unknown Source) 
    at com.google.android.gms.internal.zh.zzas(Unknown Source) 
    at com.google.android.gms.internal.zg.zzao(Unknown Source) 
    at com.google.android.gms.internal.zg.zzan(Unknown Source) 
    at com.google.firebase.database.DatabaseReference.zza(Unknown Source) 
    at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) 
    at com.example.view.TestActivity.sendDataToFirebase(TestActivity.java:43) 
    at com.example.view.TestActivity.access$000(TestActivity.java:15) 
    at com.example.view.TestActivity$1.onClick(TestActivity.java:35) 
    at android.view.View.performClick(View.java:5714) 
    at android.widget.TextView.performClick(TextView.java:10926) 
    at android.view.View$PerformClick.run(View.java:22594) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7325) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

我可以」:

package com.example.view; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 

import com.example.R; 
import com.example.model.Member; 

public class TestActivity extends AppCompatActivity { 

DatabaseReference myRef; 

Button button; 
TextView text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 

    FirebaseDatabase.getInstance().setPersistenceEnabled(true); 
    myRef = FirebaseDatabase.getInstance().getReference(); 

    text = (TextView) findViewById(R.id.text_test); 
    button = (Button) findViewById(R.id.button_test); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sendDataToFirebase(); 
     } 
    }); 
} 

private void sendDataToFirebase() { 
    String key = myRef.push().getKey(); 
    Member member = new Member("John"); 
    myRef.child("Member").child("john").child(key).setValue(member); 
} 
} 

後來我一點擊該按鈕出現此錯誤找出我的代碼有什麼問題。我遵循Firebase official website和其他教程的教程。我仔細地跟着他們,我已經嘗試了很多次。我搜索了幾個小時,但沒有希望。

希望能從這個社區得到一些解決方案或建議。 非常感謝:)

+1

'TestActivity.java:56' ...什麼是在該行? –

+0

對不起,我顯示錯誤的錯誤代碼。我編輯過它。 @ cricket_007 – Victor

+0

再次,這條線是什麼? –

回答

0

我面臨同樣的問題,當我在它的構造函數中初始化對象的元素,而不傳遞它的值。像:

public class ChatMessage { 
private String messageText; 
private String messageUser; 
private String messageTime; 
private String photoURL; 

dateFormat = new SimpleDateFormat("hh:mm aa"); 

public ChatMessage() { 
} 

public ChatMessage(String messageText, String messageUser, String photoURL) { 
    this.messageText = messageText; 
    this.messageUser = messageUser; 
    this.photoURL = photoURL; 
    **this.messageTime = dateFormat.format(new Date()).toString();** 
} 


public String getMessageText() { 
    return messageText; 
} 

public void setMessageText(String messageText) { 
    this.messageText = messageText; 
} 

public String getMessageUser() { 
    return messageUser; 
} 

public void setMessageUser(String messageUser) { 
    this.messageUser = messageUser; 
} 

public String getMessageTime() { 
    return messageTime; 
} 

public void setMessageTime(String messageTime) { 
    this.messageTime = messageTime; 
} 

public String getPhotoURL() { 
    return photoURL; 
} 

public void setPhotoURL(String photoURL) { 
    this.photoURL = photoURL; 
} 

}

然後我把它改爲:

public class ChatMessage { 
private String messageText; 
private String messageUser; 
private String messageTime; 
private String photoURL; 


public ChatMessage() { 
} 

public ChatMessage(String messageText, String messageUser, String photoURL, String messageTime) { 
    this.messageText = messageText; 
    this.messageUser = messageUser; 
    this.photoURL = photoURL; 
    this.messageTime = messageTime; 
} 


public String getMessageText() { 
    return messageText; 
} 

public void setMessageText(String messageText) { 
    this.messageText = messageText; 
} 

public String getMessageUser() { 
    return messageUser; 
} 

public void setMessageUser(String messageUser) { 
    this.messageUser = messageUser; 
} 

public String getMessageTime() { 
    return messageTime; 
} 

public void setMessageTime(String messageTime) { 
    this.messageTime = messageTime; 
} 

public String getPhotoURL() { 
    return photoURL; 
} 

public void setPhotoURL(String photoURL) { 
    this.photoURL = photoURL; 
} 
} 

使用下面MainActivity線推POJO中FireBaseDataBase:

 ChatMessage msg = new 
    ChatMessage(s,mUserName,null,dateFormat.format(new Date()).toString()); 
    mMessagesDatabaseReference.push().setValue(msg); 

這對我的作品現在有了Firebase數據庫。希望這也能幫助你。

相關問題