简体   繁体   English

无法在 Firebase 实时数据库上读写

[英]can not read or write on Firebase Realtime database

First of all I'm sorry for my English, I hope you'll understand.首先对不起我的英语,希望你能理解。 It's my first android application and I tried to use Firebase for authentication and database.这是我的第一个 android 应用程序,我尝试使用 Firebase 进行身份验证和数据库。 Authentication works fine but I can't read or write in Realtime Database.身份验证工作正常,但我无法在实时数据库中读取或写入。 These are my database rules and structure:这些是我的数据库规则和结构:

 { "rules": { ".read": true, ".write": true, } }

 { "dovelobutto": { "carta": { "cassonetto": "carta", "descrizione": "bla bla bla", "rifiuto": "carta", "valutazione": 0 } }, "prova": { "rndm": "prova" } }

I tried to add data in database to a reclyclerview but my code stop at addValueEventListener.我试图将数据库中的数据添加到 recyclerview,但我的代码在 addValueEventListener 处停止。 Then I tried to add a simple data to the Database but it didn't work too.然后我尝试向数据库添加一个简单的数据,但它也不起作用。 I also tried to put the same code in an activity but it didn't work too.我还尝试将相同的代码放入活动中,但它也不起作用。

 package com.example.genovagreen; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.SearchView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; import java.util.List; public class ButtoFragment extends Fragment { private AdapterClass adapter; private List<Oggetto> list = new ArrayList<>(); private DatabaseReference ref; private View view=null; Button button; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view=inflater.inflate(R.layout.fragment_butto, container, false); button=view.findViewById(R.id.prova); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ref=FirebaseDatabase.getInstance().getReference("prova"); ref.push().setValue("prova"); Toast.makeText(getContext(),"ok",Toast.LENGTH_LONG).show(); } }); fillList(); return view; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); fillList(); } public void fillList() { FirebaseDatabase db=FirebaseDatabase.getInstance(); ref=db.getReference("dovelobutto"); ref.addValueEventListener(new ValueEventListener() { //Never run this part @Override public void onDataChange(@NonNull DataSnapshot snapshot) { Toast.makeText(getContext(), "fillList", Toast.LENGTH_LONG).show(); for (DataSnapshot ds: snapshot.getChildren()) { String rifiuto = ds.child("rifiuto").getValue(String.class); String cassonetto = ds.child("cassonetto").getValue(String.class); Oggetto ogg = new Oggetto(rifiuto, cassonetto); list.add(ogg); } RecyclerView recyclerView = view.findViewById(R.id.rv); recyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext()); adapter = new AdapterClass(list); recyclerView.setLayoutManager(layoutManager); recyclerView.setAdapter(adapter); Toast.makeText(getContext(), "adapter settato", Toast.LENGTH_LONG).show(); } @Override public void onCancelled(@NonNull DatabaseError error) { Toast.makeText(getContext(), "errore db", Toast.LENGTH_LONG).show(); } }); } @Override public void onStart() { super.onStart(); search(); } public boolean search() { SearchView searchView=view.findViewById(R.id.searchView); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { return false; } @Override public boolean onQueryTextChange(String s) { adapter.getFilter().filter(s); return false; } }); return true; } }
These are my gradle files with all the implementations and plugins: 这些是我的 gradle 文件,其中包含所有实现和插件:

 apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 29 buildToolsVersion "30.0.2" defaultConfig { applicationId "com.example.genovagreen" minSdkVersion 21 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'net.sourceforge.jtds:jtds:1.3.1' implementation 'com.xwray:groupie:2.1.0' implementation 'com.google.android.gms:play-services-basement:17.5.0' implementation 'com.google.android.gms:play-services-flags:17.0.0' implementation platform('com.google.firebase:firebase-bom:26.1.1') implementation 'com.google.firebase:firebase-analytics' implementation 'com.google.firebase:firebase-core' implementation 'com.google.firebase:firebase-database:19.5.1' implementation 'com.google.firebase:firebase-auth' implementation 'androidx.navigation:navigation-fragment:2.3.2' implementation 'androidx.navigation:navigation-ui:2.3.2' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.1" classpath 'com.google.firebase:firebase-crashlytics-gradle:2.4.1' classpath "com.google.gms:google-services:4.3.4" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }

I also have internet permission in the manifest `.我在清单中也有互联网许可`。 This is my LogCat but the messages from the fragment where I run the Firebase code starts from the RecyclerView error.这是我的 LogCat,但我运行 Firebase 代码的片段中的消息从 RecyclerView 错误开始。 That error is because the adapter is attached after OnDataChange.该错误是因为适配器是在 OnDataChange 之后附加的。

 2020-12-18 00:04:42.535 29813-29859/com.example.genovagreen D/HostConnection: HostConnection::get() New Host Connection established 0xcade35a0, tid 29859 2020-12-18 00:04:42.537 29813-29859/com.example.genovagreen D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 2020-12-18 00:04:42.537 29813-29859/com.example.genovagreen E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1 2020-12-18 00:04:42.688 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:42.911 29813-29854/com.example.genovagreen D/FA: Connected to remote service 2020-12-18 00:04:42.912 29813-29854/com.example.genovagreen V/FA: Processing queued up service tasks: 5 2020-12-18 00:04:48.097 29813-29813/com.example.genovagreen W/ple.genovagree: Accessing hidden field Landroid/view/View;->mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (light greylist, reflection) 2020-12-18 00:04:48.175 29813-29854/com.example.genovagreen V/FA: Inactivity, disconnecting from the service 2020-12-18 00:04:48.190 29813-29854/com.example.genovagreen W/ConnectionTracker: Exception thrown while unbinding java.lang.IllegalArgumentException: Service not registered: lu@ed11e26 at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1562) at android.app.ContextImpl.unbindService(ContextImpl.java:1692) at android.content.ContextWrapper.unbindService(ContextWrapper.java:717) at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):1) at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):2) at lv.E(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):9) at lf.a(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):3) at ef.run(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):3) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at iy.run(:com.google.android.gms.dynamite_measurementdynamite@204516068@20.45.16 (100700-0):5) 2020-12-18 00:04:52.301 29813-29813/com.example.genovagreen E/RecyclerView: No adapter attached; skipping layout 2020-12-18 00:04:55.957 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:55.971 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:58.210 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:58.222 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:59.718 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130) 2020-12-18 00:04:59.733 29813-29859/com.example.genovagreen D/EGL_emulation: eglMakeCurrent: 0xe657f280: ver 2 0 (tinfo 0xe654a130)

 2020-12-21 22:30:21.031 30890-30890/com.example.genovagreen V/AudioManager: querySoundEffectsEnabled... 2020-12-21 22:30:21.069 30890-30974/com.example.genovagreen D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000 2020-12-21 22:30:21.075 30890-30890/com.example.genovagreen I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time 2020-12-21 22:30:24.548 30890-30974/com.example.genovagreen W/libEGL: EGLNativeWindowType 0x71c3397e10 disconnect failed 2020-12-21 22:31:46.137 30890-30890/com.example.genovagreen V/AudioManager: querySoundEffectsEnabled... 2020-12-21 22:31:46.165 30890-30974/com.example.genovagreen D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000 2020-12-21 22:31:46.171 30890-30890/com.example.genovagreen I/HwViewRootImpl: removeInvalidNode all the node in jank list is out of time 2020-12-21 22:31:49.650 30890-30974/com.example.genovagreen W/libEGL: EGLNativeWindowType 0x71b12424d0 disconnect failed

Add this to your build.gradle dependencies:将此添加到您的 build.gradle 依赖项中:

dependencies {
implementation 'com.google.android.gms:play-services-basement:17.4.0'
implementation 'com.google.firebase:firebase-analytics:18.0.0'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM