I try to write code to switch activity from MainActivity
to SecondActivity
. And in SecondActivity
, I initialize a FirebaseFirestore
object in onCreate()
method. I use a button in activity_main.xml to switch from MainActivity
to SecondActivity
. However, my app crashes. It did not go to the SecondActivity after I clicked the button. I really need help to solve this problem. And I need to write some data to firebase through FirebaseFirestore
object in Secondctivity
.
I think the main reason for this crash is I initialized FirebaseFirestore
object in onCreate()
method. Once I commented out this initialization, crash disappeared.
public class SecondActivity extends AppCompatActivity {
EditText name;
EditText age;
private static final String TAG = "SecondActivity";
FirebaseFirestore db;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
name = findViewById(R.id.name);
age = findViewById(R.id.age);
button = findViewById(R.id.button2);
Toast.makeText(this, "testing", Toast.LENGTH_SHORT).show();
db = FirebaseFirestore.getInstance();
}
}
The errors are the following.
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hw1, PID: 25341 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hw1/com.example.hw1.SecondActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.hw1. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.hw1. 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.firestore.FirebaseFirestore.getInstance(com.google.firebase:firebase-firestore@@18.2.0:70)
at com.example.hw1.SecondActivity.onCreate(SecondActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I/zygote: Do partial code cache collection, code=16KB, data=20KB
After code cache collection, code=16KB, data=20KB
I/zygote: Increasing code cache capacity to 128KB I/zygote: Do partial code cache collection, code=16KB, data=37KB
After code cache collection, code=16KB, data=37KB
Increasing code cache capacity to 256KB
I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.(android.content.Context, android.util.AttributeSet, int, int)
Compiler allocated 4MB to compile void android.widget.TextView.(android.content.Context, android.util.AttributeSet, int, int)
You need call
FirebaseApp.initializeApp(Context);
before
db = FirebaseFirestore.getInstance();
See this explanation of FirebaseApp
, I don't think it's needed to call initializeApp
in your code like the error log suggested, unless you're using a different Firebase project. I think the problem is either in your program's initialize process, or you have a wrong configuration for your project, for example, a wrong/corrupted google-services.json
file, a wrong package name set up in the Firebase console, or other things that are likely to cause a problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.