Firebase连接问题

时间:2019-03-01 20:41:59

标签: java android firebase firebase-realtime-database

下面的代码在我的应用程序运行之前崩溃了:

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 

App 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'

2 个答案:

答案 0 :(得分:1)

崩溃报告清楚地说明了您的代码中的问题。

  

确保首先调用FirebaseApp.initializeApp(Context)

您需要先对其进行初始化,然后再使用它,如下所示。

anko

在您的FirebaseApp.initializeApp(this); 文件中,有两个声明添加以下插件。

build.gradle

我建议您删除第一个。将其保留在gradle文件的末尾。

答案 1 :(得分:0)

感谢工作人员的所有帮助。 当我将它们更改为项目gradle中的依赖项时 类路径'com.android.tools.build:gradle:3.3.1' classpath'com.google.gms:google-services:3.0.0' 它提示我安装新的SDK文件。 不确定默认依赖项是否不起作用或下载文件是否已解决,但现在仍可以正常工作,再次感谢。