java - Firebase 连接问题
问题描述
下面的代码在我的应用程序有机会运行之前崩溃:
public class MainActivity extends AppCompatActivity {
EditText txtname,txtage,txtphone,txtheight;
Button btnsave;
DatabaseReference reff;
Schedule schedule;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtname=(EditText)findViewById(R.id.txtname);
txtage=(EditText)findViewById(R.id.txtage);
txtphone=(EditText)findViewById(R.id.txtphone);
txtheight=(EditText)findViewById(R.id.txtheight);
btnsave=(Button)findViewById(R.id.btnsave);
schedule=new Schedule();
reff=FirebaseDatabase.getInstance().getReference().child("Schedule");
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int agea=Integer.parseInt(txtage.getText().toString().trim());
Float hit=
Float.parseFloat(txtheight.getText().toString().trim());
Long phn=Long.parseLong(txtphone.getText().toString().trim());
schedule.setName(txtname.getText().toString().trim());
schedule.setAge(agea);
schedule.setHt(hit);
schedule.setPh(phn);
reff.child("schedule1").setValue(schedule);
Toast.makeText(MainActivity.this, "Data inserted successfully",
Toast.LENGTH_LONG).show();
}
});
Toast.makeText(MainActivity.this, "Firebase connection Success",
Toast.LENGTH_LONG).show();
}
}
我是 Firebase 的新手,不知道哪里出错了,代码在我的reff = firebase
行被注释掉时运行,所以我引用的方式肯定有问题,但显然,如果没有连接到数据库,代码什么也不做,如果有人可以帮忙。
按要求记录崩溃日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notihng/com.example.notihng.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.notihng. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.notihng. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
at com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-database@@16.0.6:67)
at com.example.notihng.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7210)
at android.app.Activity.performCreate(Activity.java:7201)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
... 11 more
应用 Gradle 文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.notihng"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support.constraint:constraint-
layout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.6'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation
'com.android.support.test.espresso:espresso-
core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
解决方案
崩溃报告清楚地说明了代码中的问题。
确保首先调用 FirebaseApp.initializeApp(Context)
在使用它之前,您需要先初始化它,如下所示。
FirebaseApp.initializeApp(this);
在您的build.gradle
文件中,有两个添加以下插件的声明。
apply plugin: 'com.google.gms.google-services'
我建议你删除第一个。将其保留在 gradle 文件的末尾。
推荐阅读
- reactjs - Nextjs中下一页之前的页面转换
- flutter - Flutter:Android:如何从另一个文件调用 setState()?
- python - Python jsonpath-ng:如何从 jsonpath 和值构建 json 文档?
- typescript - 打字稿地图
> “没有重载匹配这个调用”,但我不明白为什么? - javascript - 笑话测试-TypeError:无法读取 null 的属性“过滤器”
- android - 具有重叠视图的 DialogFragment
- javascript - Angular JS承诺不返回值
- android - Spring Boot/Security - 我可以使用 X509 证书作为身份验证的额外层吗?
- cisco - 具有按 AS 路径和前缀列表过滤的 Cisco BGP?
- python - 如何在 google colab 上安装 geoplot