Home > Enterprise >  Android Studio - The (Very) Basics - startActivty doesn't work
Android Studio - The (Very) Basics - startActivty doesn't work

Time:11-21

im trying to move the info of a user from one activity to the other, but everytime i run the app it crashes at the "startActivity(i)" Line (i checked everything else, it only crashes at that line).

that's the MainActivity Code

package com.example.hw1511;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hw1511.Model.User;

import org.w3c.dom.Text;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements Serializable {
    private EditText Name;
    private EditText Age;
    private EditText Birth;
    private Button AddUser;
    private Button Send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name = findViewById(R.id.Name);
        Age = findViewById(R.id.Age);
        Birth = findViewById(R.id.Birth);
        AddUser = findViewById(R.id.AddUser);
        Send = findViewById(R.id.Send);

        Intent i = new Intent(MainActivity.this, SecondActivity.class);
        List<User> list1 = new LinkedList<>();


        AddUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
              if (Name.getText().toString().trim().length() > 0 && Age.getText().toString().trim().length() > 0 && Birth.getText().toString().trim().length() > 0) {
                  User u = new User(Name.getText().toString(), Integer.parseInt(Age.getText().toString()), Birth.getText().toString());
                   list1.add(u);
                    Name.setText("");
                    Age.setText("");
                    Birth.setText("");
               }
               else {
                   Toast.makeText(MainActivity.this, "Fill All", Toast.LENGTH_SHORT).show();
               }
            }
        });

        Send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    i.putExtra("Users", (Serializable) list1);
                    startActivity(i);

                

            }
        });

    }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

and that's the SecondActivty Code (it's preety much empty)

package com.example.hw1511;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hw1511.Model.User;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.Attributes;

public class SecondActivity extends AppCompatActivity{

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

    }

}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

please if anyone help me understand what is making my app crash everytime i press the send button! it would really help me a lot.

CodePudding user response:

Your User objects need to implement Serializable as well. Also, since LinkedLists are Serializable you can use putSerializable(String key, Serializable value) instead of putExtra

  • Related