1. <i id='Rfg0H'><tr id='Rfg0H'><dt id='Rfg0H'><q id='Rfg0H'><span id='Rfg0H'><b id='Rfg0H'><form id='Rfg0H'><ins id='Rfg0H'></ins><ul id='Rfg0H'></ul><sub id='Rfg0H'></sub></form><legend id='Rfg0H'></legend><bdo id='Rfg0H'><pre id='Rfg0H'><center id='Rfg0H'></center></pre></bdo></b><th id='Rfg0H'></th></span></q></dt></tr></i><div id='Rfg0H'><tfoot id='Rfg0H'></tfoot><dl id='Rfg0H'><fieldset id='Rfg0H'></fieldset></dl></div>

    <small id='Rfg0H'></small><noframes id='Rfg0H'>

    • <bdo id='Rfg0H'></bdo><ul id='Rfg0H'></ul>

    1. <tfoot id='Rfg0H'></tfoot>

    2. <legend id='Rfg0H'><style id='Rfg0H'><dir id='Rfg0H'><q id='Rfg0H'></q></dir></style></legend>

      尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.g

      Attempt to invoke virtual method com.google.firebase.iid.FirebaseInstanceId.getInstanceId()#39; on a null object reference(尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()) - IT屋-程序员软件开发技术
      <i id='SlTIE'><tr id='SlTIE'><dt id='SlTIE'><q id='SlTIE'><span id='SlTIE'><b id='SlTIE'><form id='SlTIE'><ins id='SlTIE'></ins><ul id='SlTIE'></ul><sub id='SlTIE'></sub></form><legend id='SlTIE'></legend><bdo id='SlTIE'><pre id='SlTIE'><center id='SlTIE'></center></pre></bdo></b><th id='SlTIE'></th></span></q></dt></tr></i><div id='SlTIE'><tfoot id='SlTIE'></tfoot><dl id='SlTIE'><fieldset id='SlTIE'></fieldset></dl></div>
        1. <legend id='SlTIE'><style id='SlTIE'><dir id='SlTIE'><q id='SlTIE'></q></dir></style></legend>

              <tbody id='SlTIE'></tbody>

            <small id='SlTIE'></small><noframes id='SlTIE'>

            <tfoot id='SlTIE'></tfoot>
                <bdo id='SlTIE'></bdo><ul id='SlTIE'></ul>
              • 本文介绍了尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我将使用 firebase-messaging 库并尝试在应用启动时使用以下方法获取令牌.

                <上一页>FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener() {@覆盖公共无效 onSuccess(InstanceIdResult instanceIdResult) {字符串令牌 = instanceIdResult.getToken();//打印令牌}});

                应用程序在启动时崩溃

                java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'

                AndroidManifest

                构建.gradle:

                实施 'com.google.android.gms:play-services-analytics:16.0.5'实施 'com.google.firebase:firebase-messaging:17.3.4'

                另外,我尝试在 SharedPreferences 中缓存令牌,但似乎从未调用过 onNewToken().

                @Override公共无效onNewToken(字符串令牌){super.onNewToken(token);SharedPreferences 首选项 = PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor 编辑器 = preference.edit();editor.putString("TOKEN",token);editor.apply();}

                可能是什么问题?

                解决方案

                合并清单出现问题,合并清单中缺少以下服务.在 AndroidManifest.xml 中添加了相同的内容,它就像一个魅力.

                 <service android:name="com.google.firebase.components.ComponentDiscoveryService" ><元数据android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"android:value="com.google.firebase.components.ComponentRegistrar"/></服务>

                现在一切正常.

                I am to using firebase-messaging library and trying to fetch the token using below method on app launch.

                FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener() {
                           @Override
                           public void onSuccess(InstanceIdResult instanceIdResult) {
                               String token = instanceIdResult.getToken();
                               // print token
                           }
                       });
                

                App crashes on the launch itself giving

                java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.getInstanceId()' on a null object reference
                

                AndroidManifest

                <service
                      android:name=".MyFirebaseMessageService">
                      <intent-filter>
                            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                      </intent-filter>
                </service>
                

                Build.gradle :

                implementation 'com.google.android.gms:play-services-analytics:16.0.5'
                implementation 'com.google.firebase:firebase-messaging:17.3.4'
                

                Also, i have tried to cache the token in SharedPreferences but it seems onNewToken() is never called.

                @Override
                    public void onNewToken(String token) {
                        super.onNewToken(token);
                        SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
                        SharedPreferences.Editor editor = preference.edit();
                        editor.putString("TOKEN",token);
                        editor.apply();
                    }
                

                What could be the problem?

                解决方案

                There was problem in merged manifest, following service was missing from the merged manifest. Added same to AndroidManifest.xml it worked like a charm.

                 <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" />
                 </service>
                

                Everything is working fine now.

                这篇关于尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                Android file copy(安卓文件拷贝)
                Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)
                1. <tfoot id='P4QzZ'></tfoot>

                  <small id='P4QzZ'></small><noframes id='P4QzZ'>

                      <legend id='P4QzZ'><style id='P4QzZ'><dir id='P4QzZ'><q id='P4QzZ'></q></dir></style></legend>

                        <bdo id='P4QzZ'></bdo><ul id='P4QzZ'></ul>

                        • <i id='P4QzZ'><tr id='P4QzZ'><dt id='P4QzZ'><q id='P4QzZ'><span id='P4QzZ'><b id='P4QzZ'><form id='P4QzZ'><ins id='P4QzZ'></ins><ul id='P4QzZ'></ul><sub id='P4QzZ'></sub></form><legend id='P4QzZ'></legend><bdo id='P4QzZ'><pre id='P4QzZ'><center id='P4QzZ'></center></pre></bdo></b><th id='P4QzZ'></th></span></q></dt></tr></i><div id='P4QzZ'><tfoot id='P4QzZ'></tfoot><dl id='P4QzZ'><fieldset id='P4QzZ'></fieldset></dl></div>
                            <tbody id='P4QzZ'></tbody>