You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在线点餐APP购物车按钮异常:点击跳转至初始页面求助

Troubleshooting: Cart Button Redirects to Launch Screen After Adding Cart Code

Hey there! Let's figure out why tapping your cart button sends you back to the app's launch screen after implementing the cart functionality. Based on the Cart.java snippet you shared, here are common issues to check and fixes to try:

1. Missing or Incorrect Activity Registration in Manifest

If the Cart Activity isn't registered in your AndroidManifest.xml, the system will throw an ActivityNotFoundException and might fallback to the launch screen as a default behavior.

Fix: Double-check your manifest to ensure this entry exists:

<activity android:name=".Cart" />

2. Wrong Intent Target in Cart Button Click Logic

It’s easy to accidentally point the Intent to your launch activity (like MainActivity) instead of Cart.class when setting up the button click listener.

Verify your button click code:
Make sure it looks like this (replace CurrentActivity with the actual activity hosting the cart button):

cartButton.setOnClickListener(v -> {
    Intent cartIntent = new Intent(CurrentActivity.this, Cart.class);
    startActivity(cartIntent);
});

If you see MainActivity.class instead of Cart.class here, that’s the direct cause of the redirect.

3. Uninitialized Components in Cart Activity Causing Crash

Your Cart class declares RecyclerView, layoutManager, and adapter, but if these aren’t properly initialized in onCreate(), a runtime crash (like NullPointerException) could trigger the app to restart to the launch screen.

Add proper initialization in onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart); // Ensure this layout exists and contains the RecyclerView

    // Initialize RecyclerView
    recyclerView = findViewById(R.id.recycler_cart);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    // Initialize Firebase references
    database = FirebaseDatabase.getInstance();
    requests = database.getReference("Requests");

    // Initialize cart adapter and bind to RecyclerView
    adapter = new CartAdapter(cart, this);
    recyclerView.setAdapter(adapter);

    // Load your cart data here (from local storage or Firebase)
    loadCartItems();
}

private void loadCartItems() {
    // Add logic to populate the `cart` list with Order items
    // Example: Fetch from SharedPreferences or Firebase Realtime Database
    adapter.notifyDataSetChanged();
}

4. Check Logcat for Exact Error Details

The most reliable way to pinpoint the issue is to look at the crash logs in Android Studio’s Logcat. Filter for Error level events, then tap the cart button. You’ll see a stack trace that tells you exactly what’s failing—whether it’s a missing layout ID, uninitialized Firebase reference, or something else.


内容的提问来源于stack exchange,提问作者Ayanjit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:29:39