Android App is crashing when connecting to Firebas

2020-04-08 01:13发布

问题:

I am trying to connect to my firebase app but application is crashing when i am initializing Firebase with my app url.
The following line is giving error -
Firebase myFirebaseRef = new Firebase("https://firebase-dummy.firebaseio.com/");
It is giving IllegalAccessError.

Screenshot displaying error

package com.example.devansh.learnfirebase;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.firebase.client.utilities.DefaultRunLoop;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    Firebase myFirebaseRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Firebase.setAndroidContext(this);
        myFirebaseRef = new Firebase("https://firebase-dummy.firebaseio.com/"); //This line is causing error
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                myFirebaseRef.child("message").setValue("Do you have data? You'll love Firebase.");
            }
        });

        Button recieve = (Button) findViewById(R.id.button);
        final TextView dataStatus = (TextView) findViewById(R.id.textview);
        recieve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myFirebaseRef.child("message").addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        dataStatus.setText("Data recieved - "+snapshot.getValue());//prints "Do you have data? You'll love Firebase."
                    }

                    @Override public void onCancelled(FirebaseError error) {
                        dataStatus.setText("Data cannot be received");
                    }
                });
            }
        });
    }

Gradle file -

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.devansh.learnfirebase"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.firebase:firebase-client-android:2.5.1+'
}

Here is full text in my logcat -

02-21 00:17:37.779 27073-27073/? E/Trace: error opening trace file: No such file or directory (2)
02-21 00:17:37.829 27073-27073/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
02-21 00:17:37.829 27073-27073/? W/dalvikvm: VFY: unable to resolve interface method 17988: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
02-21 00:17:37.829 27073-27073/? W/dalvikvm: VFY: unable to resolve interface method 17992: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
02-21 00:17:37.889 27073-27073/? W/dalvikvm: VFY: unable to resolve virtual method 17884: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
02-21 00:17:37.889 27073-27073/? W/dalvikvm: VFY: unable to resolve virtual method 432: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-21 00:17:37.889 27073-27073/? W/dalvikvm: VFY: unable to resolve virtual method 454: Landroid/content/res/TypedArray;.getType (I)I
02-21 00:17:37.899 27073-27073/com.example.devansh.learnfirebase W/dalvikvm: VFY: unable to resolve direct method 18504: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
02-21 00:17:37.899 27073-27073/com.example.devansh.learnfirebase W/dalvikvm: VFY: unable to resolve direct method 18504: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
02-21 00:17:37.929 27073-27073/com.example.devansh.learnfirebase W/dalvikvm: VFY: unable to resolve virtual method 30154: Ljava/util/concurrent/ScheduledThreadPoolExecutor;.setRemoveOnCancelPolicy (Z)V
02-21 00:17:37.929 27073-27073/com.example.devansh.learnfirebase W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40e2a468)
02-21 00:17:37.929 27073-27073/com.example.devansh.learnfirebase E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   java.lang.IllegalAccessError: tried to access method java.util.concurrent.ScheduledThreadPoolExecutor.setRemoveOnCancelPolicy:()V from class com.firebase.client.utilities.DefaultRunLoop
                                                                                       at com.firebase.client.utilities.DefaultRunLoop.<init>(DefaultRunLoop.java:45)
                                                                                       at com.firebase.client.android.AndroidPlatform$1.<init>(AndroidPlatform.java:44)
                                                                                       at com.firebase.client.android.AndroidPlatform.newRunLoop(AndroidPlatform.java:44)
                                                                                       at com.firebase.client.core.Context.ensureRunLoop(Context.java:224)
                                                                                       at com.firebase.client.core.Context.initServices(Context.java:111)
                                                                                       at com.firebase.client.core.Context.freeze(Context.java:92)
                                                                                       at com.firebase.client.core.RepoManager.getLocalRepo(RepoManager.java:55)
                                                                                       at com.firebase.client.core.RepoManager.getRepo(RepoManager.java:19)
                                                                                       at com.firebase.client.Firebase.<init>(Firebase.java:172)
                                                                                       at com.firebase.client.Firebase.<init>(Firebase.java:177)
                                                                                       at com.firebase.client.Firebase.<init>(Firebase.java:155)
                                                                                       at com.example.devansh.learnfirebase.MainActivity.onCreate(MainActivity.java:30)
                                                                                       at android.app.Activity.performCreate(Activity.java:5066)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                                       at android.app.ActivityThread.access$600(ActivityThread.java:151)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                       at android.os.Looper.loop(Looper.java:155)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5559)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
                                                                                       at dalvik.system.NativeStart.main(Native Method)

回答1:

Version 2.5.1 of the Firebase SDK for Android accidentally introduced a dependency on Android API level 21.

We're working on a fix. In the meantime, you can avoid the problem by using Firebase SDK 2.5.0 or by using an Android device/emulator with API level 21 or higher.

Update: Firebase SDK for Android version 2.5.2 fixes this problem