我们尝试将Firebase数据库实现到我们的项目中,但是它一开始直接崩溃。另一个问题是我们对数据库的了解不强,但我希望我们可以解决该问题。
这是一开始就出现的例外情况
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplicationteeeeeeeeeest, PID: 19187
java.lang.NoSuchMethodError: No virtual method setTokenProvider(Lcom/google/firebase/internal/InternalTokenProvider;)V in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.myapplicationteeeeeeeeeest-N3UsgThAADh1hlowT55AfA==/base.apk)
at com.google.firebase.auth.zzp.create(Unknown Source:4)
at com.google.firebase.components.ComponentRuntime.lambda$new$0(com.google.firebase:firebase-common@@19.0.0:66)
at com.google.firebase.components.ComponentRuntime$$Lambda$1.get(Unknown Source:4)
at com.google.firebase.components.Lazy.get(com.google.firebase:firebase-common@@19.0.0:55)
at com.google.firebase.components.ComponentRuntime.initializeEagerComponents(com.google.firebase:firebase-common@@19.0.0:155)
at com.google.firebase.FirebaseApp.initializeAllApis(com.google.firebase:firebase-common@@19.0.0:553)
at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.0.0:300)
at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.0.0:264)
at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.0.0:249)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(com.google.firebase:firebase-common@@19.0.0:51)
at android.content.ContentProvider.attachInfo(ContentProvider.java:2092)
at android.content.ContentProvider.attachInfo(ContentProvider.java:2066)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@19.0.0:45)
at android.app.ActivityThread.installProvider(ActivityThread.java:6983)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6528)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6445)
at android.app.ActivityThread.access$1300(ActivityThread.java:219)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1859)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
Disconnected from the target VM, address: 'localhost:8604', transport: 'socket'
这是我们用于下载和查看数据的数据库类
package com.example.myapplicationteeeeeeeeeest;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.FirebaseApp;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplicationteeeeeeeeeest.R;
import com.example.myapplicationteeeeeeeeeest.recipe;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class ViewDatabase extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
//add Firebase Database stuff
private DatabaseReference mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private String recipes;
private ListView listView_test;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_database_layout);
//declare the database reference object. This is what we use to access the database.
//NOTE: Unless you are signed in, this will not be useable.
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance().getReference().child("testrecipe");
FirebaseUser user = mAuth.getCurrentUser();
recipes = user.getUid();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
for (DataSnapshot ds : dataSnapshot.getChildren()){
recipe recipe = new recipe();
recipe.setNameOfRecipe(ds.child(recipes).getValue(com.example.myapplicationteeeeeeeeeest.recipe.class).getNameOfRecipe());
ArrayList<String> array = new ArrayList<>();
array.add(recipe.getNameOfRecipe());
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.shopping_list_item, array);
listView_test.setAdapter(adapter);
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
/**
* customizable toast
* @param message
*/
private void toastMessage(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}
以及我们想要通过单击按钮显示信息的片段
package com.example.myapplicationteeeeeeeeeest.ui.AHome;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.myapplicationteeeeeeeeeest.MainActivity;
import com.example.myapplicationteeeeeeeeeest.R;
import com.example.myapplicationteeeeeeeeeest.ViewDatabase;
import com.google.firebase.FirebaseApp;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private Button categorie1;
private Button categorie2;
private Button categorie3;
private Button categorie4;
private Button database_button;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of( this ).get( HomeViewModel.class );
View root = inflater.inflate( R.layout.fragment_home, container, false );
categorie1 = (Button) root.findViewById(R.id.button_category_1);
categorie2 = (Button) root.findViewById(R.id.button_category_2);
categorie3 = (Button) root.findViewById(R.id.button_category_3);
categorie4 = (Button) root.findViewById(R.id.button_category_4);
database_button = (Button) root.findViewById(R.id.button_database);
initButtons();
final TextView textView = root.findViewById( R.id.text_home );
homeViewModel.getText().observe( this, new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText( s );
}
} );
return root;
}
public void initButtons(){
categorie1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
categorie2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
categorie3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
categorie4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
database_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ViewDatabase.class);
startActivity(intent);
}
});
}
}
修复第一个问题后,单击主页片段上的按钮,我们得到了一个新的问题。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplicationteeeeeeeeeest, PID: 20309
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplicationteeeeeeeeeest/com.example.myapplicationteeeeeeeeeest.ViewDatabase}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.myapplicationteeeeeeeeeest.ViewDatabase.onCreate(ViewDatabase.java:49)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 20309 SIG: 9
Disconnected from the target VM, address: 'localhost:8604', transport: 'socket'
答案 0 :(得分:0)
我猜您缺少Firebase身份验证库,因此请尝试添加
implementation 'com.google.firebase:firebase-auth:19.0.0'
在您的app/build.gradle
中。
我之前遇到过同样的错误,并添加了Firebase Auth为我修复了该错误。我在某处读到,如果您在应用程序中使用Firestore,则还需要添加Firebase身份验证库。
答案 1 :(得分:0)
首先初始化Firebase
FirebaseApp.initializeApp(getApplicationContext());