无法在Android中将数据添加到Firebase(规则设置为true)
我是Firebase的新手 我正在尝试向我的Firebase数据库添加标题和一些文本。我已将写入和读取规则设置为true。无法在Android中将数据添加到Firebase(规则设置为true),android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我是Firebase的新手 我正在尝试向我的Firebase数据库添加标题和一些文本。我已将写入和读取规则设置为true。 编辑: Copy将完全相同的代码粘贴到另一个Android Studio项目中,并建立了一个新的Firebase项目,并在我的手机上运行了完全相同的代码。它将数据添加到Firebase数据库中。 我不知道我是应该高兴还是悲伤,事实的确如此 这是要从中发送数据的类: public class CreateActivity extends AppCompatActivity {
编辑:
Copy将完全相同的代码粘贴到另一个Android Studio项目中,并建立了一个新的Firebase项目,并在我的手机上运行了完全相同的代码。它将数据添加到Firebase数据库中。 我不知道我是应该高兴还是悲伤,事实的确如此 这是要从中发送数据的类:
public class CreateActivity extends AppCompatActivity {
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mDatabaseReference;
EditText titleText;
EditText postText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("");
titleText = (EditText) findViewById(R.id.create_title);
postText = (EditText) findViewById(R.id.create_text);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mFirebaseDatabase.getReference().child("PostsText");
Button postButton = (Button) findViewById(R.id.create_button);
postButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(titleText!=null && postText!=null ) {
TextPost textPost = new TextPost(titleText.getText().toString(),postText.getText().toString());
mDatabaseReference.push().setValue(textPost).addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(CreateActivity.this,"Yes!",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(CreateActivity.this,"No!",Toast.LENGTH_SHORT).show();
}
}
});
titleText.setText("");
postText.setText("");
Toast.makeText(CreateActivity.this,"Post uploaded!",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(CreateActivity.this,"Empty fields!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
当我运行CreateActivity并按下“Post”时,toast会弹出“Post uploaded”,但控制台中我的Firebase数据库仍然显示为“null”。
此外,祝酒词“是”或“不是”也不会弹出强>
我还将google-services.json文件添加到我的应用程序文件夹中。
以下是打开CreateActivity、键入标题和文本并单击Post后的日志:
06-27 00:45:09.323 24528-24528/com.android.example.prototype I/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage,inputElapseTime=12 eventTime = 648344822 downTime = 648344822 title= com.android.example.prototype/com.android.example.prototype.CreateActivity
06-27 00:45:10.850 24528-24553/com.android.example.prototype W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-27 00:45:12.737 24528-24528/com.android.example.prototype I/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage,inputElapseTime=13 eventTime = 648348239 downTime = 648348239 title= com.android.example.prototype/com.android.example.prototype.CreateActivity
有人能帮我弄清楚这个吗?
如果你需要更多信息,请告诉我
我的build.gradle依赖项是:
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.daprlabs.aaron:swipedeck:2.0.6'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:support-v4:25.3.1'
compile 'me.grantland:autofittextview:0.2.+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
testCompile 'junit:junit:4.12'
调试器完全跳过addOnCompleteListener方法,直接将titleText和postText设置为空白。
有什么想法,我应该尝试进一步挖掘,或者我应该研究的任何特定变量吗 我重新启动了笔记本电脑,尝试写入Firebase数据库。成功了。 不知道怎么做
谢谢你的帮助 我强烈建议使用
Log
语句或调试器来确定代码流。众所周知,祝酒很容易错过。除此之外:当你运行应用程序时,你确定设备有互联网连接吗?@FrankvanPuffelen是的,我在发布时确实有互联网连接。我将尝试使用调试器。我花了将近两个小时试图找出这个问题的症结所在。当我像你一样被困在某个地方时,我开始放置日志。我几乎在代码中的任何地方都使用语句来查看代码流。我总觉得我错了。使用日志来测试流,而不是toast,这是一种更好的方法。我在调试后添加了一个图像,并将我的toast更改为日志。数据库正在获取正确的引用url,但是,它没有写入。你能推荐一些我应该检查的变量或者其他我可以尝试的想法吗?检查你的logcat输出,看看是否有提示Google Play服务过时或丢失的警告消息。
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.daprlabs.aaron:swipedeck:2.0.6'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:support-v4:25.3.1'
compile 'me.grantland:autofittextview:0.2.+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-core:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
testCompile 'junit:junit:4.12'