应用程序崩溃在FirebaseInstanceId.getInstance()。getToken()

时间:2017-07-25 15:00:29

标签: java android

我试图通过在MainActivity.java中使用FirebaseInstanceId.getInstance()。getToken()来获取令牌,但是当我使用此方法获取令牌时,应用程序星星会崩溃。

这是我的onCreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(MainActivity.this, FirebaseInstanceId.getInstance().getToken(), Toast.LENGTH_SHORT).show();
}

Build.gradle(项目级别)

 buildscript {
        repositories {
            jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Build.gradle(应用级别)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.simple.app"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })






    compile ('com.android.support:appcompat-v7:25.0.1')
            {
                force=true
            }
    compile ('com.android.support.constraint:constraint-layout:1.0.2')
            {
                force=true
            }

    compile ('com.android.support:design:25.0.1')
            {
                force=true
            }
    compile 'com.firebaseui:firebase-ui:0.6.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
    compile ('com.android.support:support-v4:25.3.1')
            {
                force=true
            }
    compile ('com.google.firebase:firebase-messaging:9.2.0')
            {
                force=true
            }

    testCompile 'junit:junit:4.12'}
apply plugin: 'com.google.gms.google-services'

logcat的

07-25 20:25:20.903 4563-4563/com.simple.app D/AndroidRuntime: Shutting down VM
07-25 20:25:20.907 4563-4563/com.simple.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.simple.app, PID: 4563
                                                                           java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzx; or its super classes (declaration of 'com.google.android.gms.common.util.zzx' appears in /data/app/com.simple.app-2/split_lib_dependencies_apk.apk:classes42.dex)
                                                                               at com.google.firebase.iid.zzg.zzkg(Unknown Source)
                                                                               at com.google.firebase.iid.zzg.<init>(Unknown Source)
                                                                               at com.google.firebase.iid.zzg.<init>(Unknown Source)
                                                                               at com.google.firebase.iid.zzd.zzb(Unknown Source)
                                                                               at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                               at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                               at com.simple.app.MainActivity.onCreate(MainActivity.java:138)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:1)

  

java.lang.NoSuchMethodError:没有静态方法   getNoBackupFilesDir(Landroid /内容/上下文;)Ljava / IO /文件;在班上   LCOM /谷歌/机器人/克/普通/ util的/ ZZX;

<强> FYI

ASAIK android.support.v4.content.ContextCompat. 出现在 support-v4:25.3.1

  • build.gradle 依赖关系部分删除 Clean-Rebuild and Run
  • 然后 android { // ... compileSdkVersion 26 buildToolsVersion '26.0.1' defaultConfig { targetSdkVersion 26 } // ... }

如果使用最新版本会更好。

repositories {
    maven { url 'https://maven.google.com' }
}

添加

dependencies {
    // ...
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:support-compat:26.1.0'
    compile 'com.google.firebase:firebase-messaging:11.4.2'
    // ...
}

FirebaseInstanceId.getInstance().getToken()
  

注意

     

onTokenRefreshcallback 调用以检索当前令牌。如果尚未创建令牌,则为null   返回。

监控令牌生成

正如关于Set up a Firebase cloud messaging client app on Android.的官方指南所示:

  

“只要生成新令牌,getToken就会触发,因此在其上下文中调用getToken可确保您访问当前可用的注册令牌。请确保已将服务添加到{ {3}},然后在onTokenRefresh的上下文中调用public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService"; /** * Called if InstanceID token is updated. This may occur if the security of * the previous token had been compromised. Note that this is called when the InstanceID token * is initially generated so this is where you would retrieve the token. */ @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // Instance ID token to your app server. sendRegistrationToServer(refreshedToken); } // ... } ,并记录值(...)“

实施例

#formWrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100px;
}

input {
  height: 12px;
  transform-origin: 50% left;
    -webkit-transition: .5s ease-out;
    -moz-transition: .5s ease-out;
    -o-transition: .5s ease-out;
    transition: .5s ease-out;
}

input:focus {
 height: 50px;
}

答案 1 :(得分:0)

尝试使用相同版本的firebase SDK并支持SDK。

检查依赖关系树:

./gradlew :{yourappmodulename}:dependencies --configuration compile

如果您检查树,您将看到不同的版本:

  • Firebase:9.2.0和9.8.0
  • 支持SDK:25.0.1和25.3.1

如果您将firebase更新到9.8.0并支持SDK到25.3.1,则可以修复它。

答案 2 :(得分:0)

将以下几行添加到您的AndroidManifest.xml

<service android:name="com.google.firebase.components.ComponentDiscoveryService" >
        <meta-data
            android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
            android:value="com.google.firebase.components.ComponentRegistrar" />

在我的情况下有效。...