请务必先在Android中调用FirebaseApp.initializeApp(上下文)

请务必先在Android中调用FirebaseApp.initializeApp(上下文),第1张

概述我正面临这个问题,并在这个网站上看到了一些答案,但没有得到任何适当的解决方案.我使用过以前版本的Firebase,但是当我尝试使用Upgradation升级并将Firebase类更新为DatabaseReference时,它显示错误而无法正常工作.我将整个代码添加到我的清单文件中,所以请帮我解决这个问题.这是

我正面临这个问题,并在这个网站上看到了一些答案,但没有得到任何适当的解决方案.
我使用过以前版本的Firebase,但是当我尝试使用Upgradation升级并将Firebase类更新为DatabaseReference时,它显示错误而无法正常工作.
我将整个代码添加到我的清单文件中,所以请帮我解决这个问题.
这是我的清单

    <?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="firebasechat.com.firebasechat">    <uses-permission androID:@R_419_6889@="androID.permission.INTERNET" />    <application        androID:allowBackup="true"        androID:@R_419_6889@=".Activity.SimpleBlog"        androID:icon="@mipmap/ic_launcher"        androID:label="@string/app_@R_419_6889@"        androID:roundIcon="@mipmap/ic_launcher_round"        androID:supportsRtl="true"        androID:theme="@style/Apptheme">        <activity androID:@R_419_6889@=".Activity.Registeractivity">            <intent-filter>                <action androID:@R_419_6889@="androID.intent.action.MAIN" />                <category androID:@R_419_6889@="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

我的模块应用程序如下.

    apply plugin: 'com.androID.application'androID {    compileSdkVersion 26    buildToolsversion "26.0.0"    defaultConfig {        applicationID "firebasechat.com.firebasechat"        minSdkVersion 15        targetSdkVersion 26        versionCode 1        version@R_419_6889@ "1.0"        testInstrumentationRunner "androID.support.test.runner.AndroIDJUnitRunner"        multIDexEnabled  true    }    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(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:26.+'compile 'com.androID.volley:volley:1.0.0'compile "com.Google.firebase:firebase-database:11.0.0"compile 'com.Google.androID.gms:play-services:11.0.0'compile 'com.androID.support:recyclervIEw-v7:25.0.0'testCompile 'junit:junit:4.12'    testCompile 'junit:junit:4.12'}

和项目gradle

    // top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositorIEs {        jcenter()    }    dependencIEs {        classpath 'com.androID.tools.build:gradle:2.3.3'        classpath 'com.Google.gms:Google-services:4.2.0'// Updated version of Google service    }}allprojects {    repositorIEs {        jcenter()        maven {            url "https://maven.Google.com" // Google's Maven repository        }    }}task clean(type: Delete) {    delete rootProject.buildDir}

以下是我的活动.

    public class Registeractivity extends AppCompatActivity {    EditText user@R_419_6889@, password;    button registerbutton;    String user, pass;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_register);        user@R_419_6889@ = (EditText)findVIEwByID(R.ID.user@R_419_6889@);        password = (EditText)findVIEwByID(R.ID.password);        registerbutton = (button)findVIEwByID(R.ID.registerbutton);          FirebaseApp.initializeApp(this);        registerbutton.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                user = user@R_419_6889@.getText().toString();                pass = password.getText().toString();                    final ProgressDialog pd = new ProgressDialog(Registeractivity.this);                    pd.setMessage("Loading...");                    pd.show();                    String url = "https://pure-coda-174710.firebaseio.com/users.Json";                    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){                        @OverrIDe                        public voID onResponse(String s) {//                            Firebase reference = new Firebase("https://pure-coda-174710.firebaseio.com/users");                            DatabaseReference reference = FirebaseDatabase.getInstance()                                    .getReferenceFromUrl("https://pure-coda-174710.firebaseio.com/users");                            if(s.equals("null")) {                                reference.child(user).child("password").setValue(pass);                                Toast.makeText(Registeractivity.this, "registration successful", Toast.LENGTH_LONG).show();                            }                            else {                                try {                                    JsONObject obj = new JsONObject(s);                                    if (!obj.has(user)) {                                        reference.child(user).child("password").setValue(pass);                                        Toast.makeText(Registeractivity.this, "registration successful", Toast.LENGTH_LONG).show();                                    } else {                                        Toast.makeText(Registeractivity.this, "user@R_419_6889@ already exists", Toast.LENGTH_LONG).show();                                    }                                } catch (JsONException e) {                                    e.printstacktrace();                                }                            }                            pd.dismiss();                        }                    },new Response.ErrorListener(){                        @OverrIDe                        public voID one rrorResponse(VolleyError volleyError) {                            System.out.println("" + volleyError );                            pd.dismiss();                        }                    });                    RequestQueue rQueue = Volley.newRequestQueue(Registeractivity.this);                    rQueue.add(request);            }        });    }}

解决方法:

在SimpleBlog应用程序类中,在onCreate()方法中初始化FirebaseApp并将其从Registeractivity中删除,以使Firebase初始化为整个应用程序,而不仅仅是一个Activity.

@OverrIDepublic voID onCreate() {    super.onCreate();    FirebaseApp.initializeApp(this);}

同时在app gradle结束时添加apply plugin:’com.Google.gms.Google-services’:

dependencIEs {    ....}apply plugin: 'com.Google.gms.Google-services'

需要插件来处理来自firebase的Json配置并避免依赖性冲突.您可以阅读here以获取更多详细信息.

总结

以上是内存溢出为你收集整理的请务必先在Android中调用FirebaseApp.initializeApp(上下文)全部内容,希望文章能够帮你解决请务必先在Android中调用FirebaseApp.initializeApp(上下文)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存