gpt4 book ai didi

android - 在我的应用程序中使用按钮会导致它崩溃

转载 作者:行者123 更新时间:2023-11-30 05:02:59 25 4
gpt4 key购买 nike

所以我和我的 friend 们正在开发一个带有 firebase 数据库的应用程序。我们是实现和读取数据库的新手,所以我想测试一下,看看我的方法是否可行。我创建了一个 ViewDatabase 类来尝试它,我想有意地开始这个 Activity 。带有 Intent 的按钮是从一个 fragment 开始的。我可以启动该应用程序,但是当我按下按钮时它就崩溃了。我找不到让它工作的方法,所以我希望这里有人对我的问题有所了解。

这是 ViewDatabase 类。

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();
}

这是 intentbutton 所在的 fragment 。

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(){

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: 3799
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:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
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:50)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

最佳答案

处理没有用户帐户的可能性,将他们引导至登录屏幕。

FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
recipes = user.getUid();
} else {
// Ask user to login and repeat the logic to getUid();
}

关于android - 在我的应用程序中使用按钮会导致它崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57894074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com