Android 在我的应用程序中使用按钮会导致它崩溃
因此,我和我的朋友正在开发一款带有firebase数据库的应用程序。 我们对实现和读取数据库还不熟悉,所以我想测试一下,如果我的方法可行的话。我创建了一个ViewDatabase类来尝试它,我想带着一个意图开始这个活动。具有该意图的按钮从片段中启动。我可以启动应用程序,但当我按下按钮时,它就崩溃了。我找不到办法让它工作,所以我希望这里的人对我的问题有一些了解 这是ViewDatabase类Android 在我的应用程序中使用按钮会导致它崩溃,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,因此,我和我的朋友正在开发一款带有firebase数据库的应用程序。 我们对实现和读取数据库还不熟悉,所以我想测试一下,如果我的方法可行的话。我创建了一个ViewDatabase类来尝试它,我想带着一个意图开始这个活动。具有该意图的按钮从片段中启动。我可以启动应用程序,但当我按下按钮时,它就崩溃了。我找不到办法让它工作,所以我希望这里的人对我的问题有一些了解 这是ViewDatabase类 public class ViewDatabase extends AppCompatActivity {
public class ViewDatabase extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
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);
FirebaseApp.initializeApp(this);
//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("category");
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());
recipe.setCategoryOfRecipe(ds.child(recipes).getValue(com.example.myapplicationteeeeeeeeeest.recipe.class).getCategoryOfRecipe());
recipe.setDescriptionOfRecipe(ds.child(recipes).getValue(com.example.myapplicationteeeeeeeeeest.recipe.class).getDescriptionOfRecipe());
recipe.setIDOfRecipe(ds.child(recipes).getValue(com.example.myapplicationteeeeeeeeeest.recipe.class).getIDOfRecipe());
ArrayList<String> array = new ArrayList<>();
array.add(recipe.getNameOfRecipe());
array.add(recipe.getDescriptionOfRecipe());
array.add(recipe.getCategoryOfRecipe());
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();
}
处理可能没有用户帐户,将他们引导到登录屏幕
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
recipes = user.getUid();
} else {
// Ask user to login and repeat the logic to getUid();
}
处理可能没有用户帐户,将他们引导到登录屏幕
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
recipes = user.getUid();
} else {
// Ask user to login and repeat the logic to getUid();
}
崩溃日志在哪里?这里崩溃,
recipes=user.getUid()代码>。您没有登录到Firebase帐户。崩溃日志在哪里?这里崩溃,recipes=user.getUid()代码>。您没有登录到Firebase帐户谢谢!我原以为这种方法会带来一些麻烦,但这让我意识到我有一个不同的问题。我的应用程序中没有任何用户帐户或其他内容。我需要他们来管理firebase吗?因为我只是用它来尝试在firebase中指向我的列,但现在我知道这并不能解决问题。firebase身份验证和firebase数据库是独立的实体。可能有Firebase数据库的登录名或某种令牌将您的应用程序链接到它。谢谢!我原以为这种方法会带来一些麻烦,但这让我意识到我有一个不同的问题。我的应用程序中没有任何用户帐户或其他内容。我需要他们来管理firebase吗?因为我只是用它来尝试在firebase中指向我的列,但现在我知道这并不能解决问题。firebase身份验证和firebase数据库是独立的实体。可能有Firebase数据库的登录名或某种令牌将您的应用程序链接到它。
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
recipes = user.getUid();
} else {
// Ask user to login and repeat the logic to getUid();
}