2

I am making a simple Android app. I have 3 classes MainActivity, PlayerDetailActivity and Player. It was an error no suitable method found for putExtra. Then I added implements Serializable into my Player class. Now program compiles but gives me an empty screen. It seems to me that something wrong with my intent.

MainActivity class

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
List<Player> bostonCeltics = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bostonCeltics.add(new Player("Kyrie Irving", "23-Mar-92", 190.5, "Point guard", R.mipmap.irving));
    bostonCeltics.add(new Player("Jayson Tatum", "3-Mar-98", 190.5, "Small forward", R.mipmap.tatum));
    bostonCeltics.add(new Player("Gordon Hayward", "23-Mar-90", 203.2, "Small forward", R.mipmap.hayward));
    bostonCeltics.add(new Player("Robert Williams", "17-Oct-97", 208.3, "Power forward", R.mipmap.williams));
    bostonCeltics.add(new Player("Jabari Bird", "3-Jul-94", 198.1, "Shooting guard", R.mipmap.bird));
    bostonCeltics.add(new Player("Al Horford", "3-Jun-86", 208.3, "Center / Power forward", R.mipmap.horford));
    bostonCeltics.add(new Player("Jaylen Brown", "24-Oct-96", 200.7, "Small forward/ Shooting guard", R.mipmap.brown));
    bostonCeltics.add(new Player("Marcus Smart", "6-Mar-94", 193.0, "Shooting guard / Point guard", R.mipmap.smart));
    bostonCeltics.add(new Player("Terry Rozier", "17-Mar-94", 185.4, "Point guard", R.mipmap.rozier));
    bostonCeltics.add(new Player("Aron Baynes", "9-Dec-86", 208.3, "Center / Power forward", R.mipmap.baynes));
    bostonCeltics.add(new Player("Marcus Morris", "2-Sep-89", 205.7, "Forward", R.mipmap.morris));
    bostonCeltics.add(new Player("Brad Wanamaker", "25-Jul-89", 193.0, "Point guard / Shooting guard", R.mipmap.wanamaker));
    bostonCeltics.add(new Player("P. J. Dozier", "25-Oct-96", 198.1, "Shooting guard", R.mipmap.dozier));
    bostonCeltics.add(new Player("Daniel Theis", "4-Apr-92", 203.2, "Center / Power forward", R.mipmap.theis));
    bostonCeltics.add(new Player("Guerschon Yabusele", "17-Dec-95", 203.2, "Power forward", R.mipmap.yabusele));
    bostonCeltics.add(new Player("Semi Ojeleye", "5-Dec-94", 200.7, "Forward", R.mipmap.ojeleye));
    bostonCeltics.add(new Player("Walt Lemon Jr.", "26-Jul-92", 190.5, "Point guard", R.mipmap.lemon));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent moveToDetailIntent = new Intent(getBaseContext(), PlayerDetailActivity.class);
    moveToDetailIntent.putExtra("player", bostonCeltics.get(position));
    startActivity(moveToDetailIntent);
}

Player Class

enter codepublic class Player implements Serializable {
private String name;
private String age;
private double height;
private String position;
private int image;
Player(String name, String age, double height, String position, int image) {
    this.name = name;
    this.age = age;
    this.height = height;
    this.position = position;
    this.image = image;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public double getHeight() {
    return height;
}
public void setHeight(double height) {
    this.height = height;
}
public String getPosition() {
    return position;
}
public void setPosition(String position) {
    this.position = position;
}
public int getImage() {
    return image;
}
public void setImage(int image) {
    this.image = image;
}}

PlayerDetailActivity class

public class PlayerDetailActivity extends AppCompatActivity {

TextView nameTextView;
TextView ageTextView;
TextView heightTextView;
TextView positionTextView;
ImageView imageView;

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

    nameTextView = (TextView) findViewById(R.id.nameTextView);
    ageTextView = (TextView) findViewById(R.id.ageTextView);
    heightTextView = (TextView) findViewById(R.id.heightTextView);
    positionTextView = (TextView) findViewById(R.id.positionTextView);
    imageView = (ImageView) findViewById(R.id.imageView);

    Player player = (Player) getIntent().getExtras().get("player");

    nameTextView.setText(player.getName());
    ageTextView.setText("Birth date: " + player.getAge());
    heightTextView.setText("Height: " + player.getHeight());
    positionTextView.setText("Position: " + player.getPosition());
    imageView.setImageResource(player.getImage());
}}

Manifest

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".PlayerDetailActivity" />
    <activity android:name=".Main2Activity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".IntroActivity"></activity>
</application>

Dale K
  • 16,372
  • 12
  • 37
  • 62
Koke Abeke
  • 57
  • 5
  • After adding `implements Serializable` do you get any other errors? I suggest that you use the Android Studio debugger to step through your code to see what is happening. – Code-Apprentice Dec 12 '18 at 17:28
  • this code should work. Maybe your app is retaining intents from other invocations. Can you also post the `Activity` declaration in the manifest? – nandsito Dec 12 '18 at 18:19

1 Answers1

1

I'll suggest you use Parcelable instead of Serializable because Parcelable process is much faster.

Player.java

import android.os.Parcel;
import android.os.Parcelable;

public class Player implements Parcelable {

    private String name;
    private String age;
    private double height;
    private String position;
    private int image;

    Player(String name, String age, double height, String position, int image) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.position = position;
        this.image = image;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public String getPosition() {
        return position;
    }
    public void setPosition(String position) {
        this.position = position;
    }
    public int getImage() {
        return image;
    }
    public void setImage(int image) {
        this.image = image;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
        dest.writeString(this.age);
        dest.writeDouble(this.height);
        dest.writeString(this.position);
        dest.writeInt(this.image);
    }

    protected Player(Parcel in) {
        this.name = in.readString();
        this.age = in.readString();
        this.height = in.readDouble();
        this.position = in.readString();
        this.image = in.readInt();
    }

    public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
        @Override
        public Player createFromParcel(Parcel source) {
            return new Player(source);
        }

        @Override
        public Player[] newArray(int size) {
            return new Player[size];
        }
    };
}

Now you can send this object to other activity using Intent:

Intent intent = new Intent(context, PlayerDetailActivity.class);
intent.putExtra("player", bostonCeltics.get(position));
startActivity(intent);

In PlayerDetailActivity class use this to extract data:

PlayerDetailActivity player = getIntent().getExtras().getParcelable("player");