现在,我只是将Firebase实时数据库放入了我的应用程序,每当我打开一个新的Activity包含DatabaseReference时,我的应用程序就会崩溃。有什么建议可以解决这个问题吗?
我遵循了Firebase Realtime Database的教程。网址:https://www.youtube.com/watch?v=EM2x33g4syY&t=3s
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_editor);
dbr = FirebaseDatabase.getInstance().getReference("title");
final TableLayout tl = (TableLayout) findViewById(R.id.tbllays);
String[] teamRoles = {"Director", "Marketing", "Team", "All"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, teamRoles);
context = getApplicationContext();
//adds new row
Button addnewrow = (Button) findViewById(R.id.AddRow);
addnewrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//creates table
final TableRow tbr = new TableRow(context);
TableRow.LayoutParams layoutParams = new
TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
tbr.setLayoutParams(layoutParams);
//add edittext to name the task
EditText editText = new EditText(context);
editText.setHint("Add Task");
tbr.addView(editText, 0);
//add spinner to assign teams
Spinner toDo = new Spinner(context);
toDo.setAdapter(adapter);
tbr.addView(toDo, 1);
//add spinner to assign teams
Spinner InProgress = new Spinner(context);
InProgress.setAdapter(adapter);
tbr.addView(InProgress, 2);
//add spinner to assign teams
Spinner Test = new Spinner(context);
Test.setAdapter(adapter);
tbr.addView(Test, 3);
//add spinner to assign teams
Spinner Done = new Spinner(context);
Done.setAdapter(adapter);
tbr.addView(Done, 4);
// Add a button in the second column
Button button = new Button(context);
button.setText("X");
tbr.addView(button, 5);
button.setBackgroundColor(Color.rgb(66, 244, 125));
// Get delete table row button.
Button deleteRowButton = (Button) button;
deleteRowButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
tbr.removeAllViews();
}
});
tl.addView(tbr);
}
});
title = (EditText) findViewById(R.id.TitleofDoc);
createBtn = (Button) findViewById(R.id.SaveThis);
createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//AddNewTasks();
}
});
}
private void AddNewTasks() {
String nitle = title.getText().toString().trim();
if(!TextUtils.isEmpty(nitle)){
String id = dbr.push().getKey();
Title bible = new Title(nitle, id);
dbr.child(id).setValue(bible);
Toast.makeText(context, "File Created.",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Put a Title...",
Toast.LENGTH_SHORT).show();
}
}
标题POJO:
public class Title {
String title;
String idon;
public Title(){
}
public Title(String title, String idon) {
this.title = title;
this.idon = idon;
}
public Title(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public String getIdon() {
return idon;
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.scrumanager2, PID: 13271
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scrumanager2/com.example.scrumanager2.MainEditor}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.scrumanager2. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.scrumanager2. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.1.0:245)
at com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-database@@16.1.0:67)
at com.example.scrumanager2.MainEditor.onCreate(MainEditor.java:54)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)