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

Android中startActivity跳转崩溃求助:传递User类型ArrayList失败

Fixing Your startActivity() Crash When Passing ArrayList

Hey there, let's break down why your app is crashing when trying to launch ScenedActivity, and fix it step by step.

Core Crash Cause: Your User Class Isn't Serializable

The biggest issue here is that you're trying to pass an ArrayList<User> via Intent, but the User class doesn't implement a serialization interface. Android requires any object passed through Intent extras to be either Serializable or Parcelable—without this, the system throws a NotSerializableException which crashes your app immediately.

Fix: Make User Implement Serializable

Update your User class to implement the Serializable interface (this is the simpler option for basic objects):

public class User implements Serializable {
    // Add your class fields here
    private String firstName;
    private String lastName;
    private String game;
    private int code;

    // Constructor
    public User(String firstName, String lastName, String game, int code) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.game = game;
        this.code = code;
    }

    // Optional but recommended: Add a serialVersionUID to avoid deserialization issues
    private static final long serialVersionUID = 1L;

    // Add getters/setters if needed
}

Secondary Issue: Using System Category Strings as Extra Keys

You're using android.intent.category.LAUNCHER and android.intent.category.LAUNCHER1 as keys for your Intent extras. These are system-defined category values, and reusing them can cause conflicts or unexpected behavior. Always use custom, unique keys for your extras.

Fix: Define Custom Extra Keys

In your MainActivity, add constants for your extra keys:

// Replace with your app's package name to ensure uniqueness
private static final String EXTRA_GREETING = "com.yourappname.extra.GREETING";
private static final String EXTRA_USER_LIST = "com.yourappname.extra.USER_LIST";

Then update your Intent code to use these keys:

Intent StartIntent = new Intent(getApplicationContext(), ScenedActivity.class);
StartIntent.putExtra(EXTRA_GREETING, "Hi");
StartIntent.putExtra(EXTRA_USER_LIST, users);
startActivity(StartIntent);

Receiving the Extras in ScenedActivity

In ScenedActivity, retrieve the extras using the same keys:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scened);

    Intent incomingIntent = getIntent();
    String greeting = incomingIntent.getStringExtra(MainActivity.EXTRA_GREETING);
    ArrayList<User> receivedUsers = (ArrayList<User>) incomingIntent.getSerializableExtra(MainActivity.EXTRA_USER_LIST);

    // Use the data as needed
}

Minor Code Optimizations

Let's clean up a few other parts of your code to avoid future issues:

  • Simplify the isEmpty method: Use Android's built-in TextUtils for cleaner checks:
    private boolean isEmpty(EditText etText) {
        return TextUtils.isEmpty(etText.getText().toString().trim());
    }
    
  • Avoid static variables for cod: Static variables persist in memory even after your activity is destroyed, which can lead to memory leaks or unexpected state. Use a local variable instead:
    int cod = 0;
    if (!isEmpty(Cod)) {
        cod = Integer.parseInt(Cod.getText().toString());
    }
    // No need for an else block—cod is already initialized to 0
    

Once you implement these fixes, your startActivity() call should work without crashing, and your User list will be successfully passed to ScenedActivity.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:46:05