无法读取或写入firebase实时数据库

xxls0lw8  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(358)

首先我为我的英语感到抱歉,希望你能理解。这是我的第一个android应用程序,我尝试使用firebase进行身份验证和数据库。身份验证工作正常,但我不能读取或写入实时数据库。以下是我的数据库规则和结构:

{
  "rules": {
    ".read": true,
    ".write": true,
  }
}
{
  "dovelobutto" : {
    "carta" : {
      "cassonetto" : "carta",
      "descrizione" : "bla bla bla",
      "rifiuto" : "carta",
      "valutazione" : 0
    }
  },
  "prova" : {
    "rndm" : "prova"
  }
}

我试图将数据库中的数据添加到recyclerview,但代码在addvalueeventlistener处停止。然后我尝试向数据库中添加一个简单的数据,但效果不太好。我也尝试在活动中使用相同的代码,但效果不太好。

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;
    }

}

这些是我的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
}

我在清单中也有互联网权限。这是我的logcat,但是我运行firebase代码的片段中的消息是从recyclerview错误开始的。该错误是因为适配器是在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
p1iqtdky

p1iqtdky1#

将此添加到build.gradle依赖项:

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

相关问题