我似乎无法理解为什么无法写入数据库。在我看来,存在一些我无法解决的连接问题,因为我确实遵循了有关此问题的一些教程,但没有一个获得成功。
这里我已经检查了几个关键点,但是我想我还缺少其他东西...
这是我的Firebase实时数据库,只有一个孩子:
{
"message" : "nil"
}
这是我的Firebase实时数据库规则:
{
"rules": {
".read": true,
".write": true
}
}
这是我的项目Gradle依赖项:
...
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.google.gms:google-services:4.2.0'
}
...
这是我的模块Gradle依赖项:
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-database:18.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.0.0'
}
apply plugin: 'com.google.gms.google-services'
...
我在Android.Manifest.xml中添加了INTERNET权限
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
...
还有我来自MainActivity.java的实际代码
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference();
public DatabaseReference cinemasRef = myRootRef.child("message");
@Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar;
ActionBarDrawerToggle toggle;
cinemasRef.setValue("I logged");
...
}
}
我希望每当我启动应用程序时,因为我已将setValue放在onCreate()方法中,所以我尊敬的数据库/子级中的值(例如“ nil”)将被“我记录”所覆盖。 由于我已经坚持了好几个星期了,所以不断重复一些帖子/教程/要点,因此将非常感谢一些指示或想法。谢谢!
编辑#1 这是我的Logcat
2019-08-09 16:48:30.840 9474-9474/? D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2019-08-09 16:48:30.847 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2019-08-09 16:48:30.849 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2019-08-09 16:48:30.851 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2019-08-09 16:53:12.302 9474-9474/? I/example.scratc: Not late-enabling -Xcheck:jni (already on)
2019-08-09 16:53:12.336 9474-9474/? E/example.scratc: Unknown bits set in runtime_flags: 0x8000
2019-08-09 16:53:12.337 9474-9474/? W/example.scratc: Unexpected CPU variant for X86 using defaults: x86
2019-08-09 16:53:13.019 9474-9474/com.example.scratch W/example.scratc: JIT profile information will not be recorded: profile file does not exits.
2019-08-09 16:53:13.022 9474-9474/com.example.scratch I/chatty: uid=10135(com.example.scratch) identical 10 lines
2019-08-09 16:53:13.022 9474-9474/com.example.scratch W/example.scratc: JIT profile information will not be recorded: profile file does not exits.
2019-08-09 16:53:13.435 9474-9474/com.example.scratch V/FA: Registered activity lifecycle callback
2019-08-09 16:53:13.495 9474-9474/com.example.scratch I/FirebaseInitProvider: FirebaseApp initialization successful
2019-08-09 16:53:13.499 9474-9474/com.example.scratch I/InstantRun: starting instant run server: is main process
2019-08-09 16:53:13.691 9474-9474/com.example.scratch D/DEBUG: THIS IS ONCREATE ????????????????????????????????????
2019-08-09 16:53:13.693 9474-10282/com.example.scratch V/FA: Collection enabled
2019-08-09 16:53:13.693 9474-10282/com.example.scratch V/FA: App package, google app id: com.example.scratch, 1:801495270237:android:57be8c2d1041efb4
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: App measurement is starting up, version: 16250
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.scratch
2019-08-09 16:53:13.702 9474-10282/com.example.scratch D/FA: Debug-level message logging enabled
2019-08-09 16:53:13.731 9474-9474/com.example.scratch V/FA: onActivityCreated
2019-08-09 16:53:13.776 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2019-08-09 16:53:13.777 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2019-08-09 16:53:13.784 9474-10289/com.example.scratch D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-08-09 16:53:13.925 9474-10282/com.example.scratch V/FA: Connecting to remote service
2019-08-09 16:53:13.993 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->doBeforeTextChanged()V (greylist-max-p, reflection, denied)
2019-08-09 16:53:13.993 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->doAfterTextChanged()V (greylist-max-p, reflection, denied)
2019-08-09 16:53:13.994 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->ensureImeVisible(Z)V (greylist-max-p, reflection, denied)
2019-08-09 16:53:14.006 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.211 9474-10282/com.example.scratch I/FA: Tag Manager is not found and thus will not be used
2019-08-09 16:53:14.219 9474-10282/com.example.scratch D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=5326573072556222733}]
2019-08-09 16:53:14.271 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.271 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.277 9474-10282/com.example.scratch V/FA: Activity resumed, time: 50397964
2019-08-09 16:53:14.334 9474-10286/com.example.scratch D/OpenGLRenderer: Swap behavior 1
2019-08-09 16:53:14.336 9474-10286/com.example.scratch W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-08-09 16:53:14.336 9474-10286/com.example.scratch D/OpenGLRenderer: Swap behavior 0
2019-08-09 16:53:14.347 9474-10286/com.example.scratch D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2019-08-09 16:53:14.347 9474-10286/com.example.scratch D/EGL_emulation: eglCreateContext: 0xe39e4880: maj 3 min 0 rcv 3
2019-08-09 16:53:14.375 9474-10286/com.example.scratch D/EGL_emulation: eglMakeCurrent: 0xe39e4880: ver 3 0 (tinfo 0xd81e8170)
2019-08-09 16:53:14.525 9474-10286/com.example.scratch W/Gralloc3: mapper 3.x is not supported
2019-08-09 16:53:14.600 9474-10286/com.example.scratch D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0
2019-08-09 16:53:14.674 9474-10286/com.example.scratch D/EGL_emulation: eglMakeCurrent: 0xe39e4880: ver 3 0 (tinfo 0xd81e8170)
2019-08-09 16:53:14.683 9474-10286/com.example.scratch D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2019-08-09 16:53:14.866 9474-9474/com.example.scratch D/DEBUG: THIS IS ONCREATEOPTIONMENU!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2019-08-09 16:53:14.875 9474-9474/com.example.scratch I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
2019-08-09 16:53:14.900 9474-10282/com.example.scratch D/FA: Connected to remote service
2019-08-09 16:53:14.902 9474-10282/com.example.scratch V/FA: Processing queued up service tasks: 4
2019-08-09
答案 0 :(得分:1)
尝试这样做:
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public DatabaseReference myRootRef, cinemasRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar;
ActionBarDrawerToggle toggle;
// Moving the initialisation in function
myRootRef = FirebaseDatabase.getInstance().getReference();
cinemasRef = myRootRef.child("message");
cinemasRef.setValue("I logged");
...
}
}
一个Codelab示例:https://codelabs.developers.google.com/codelabs/firebase-android/#1