我正在制作一个简单的Android应用程序。我有三个类MainActivity
、PlayerDetailActivity
和Player
。这是一个错误,找不到适用于putExtra
的方法。然后我将implements Serializable
添加到我的Player
类中。现在程序正在编译,但给了我一个空屏幕。在我看来,我的意图出了问题。
主活动类
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);
}
玩家等级
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类
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());
}}
舱单
<?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>
我建议您使用Parcelable而不是Serializable,因为Parcelable进程要快得多。
- Parcelable和Serializable之间的区别
- 如何使用Parcelable
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];
}
};
}
现在,您可以使用Intent:将此对象发送到其他活动
Intent intent = new Intent(context, PlayerDetailActivity.class);
intent.putExtra("player", bostonCeltics.get(position));
startActivity(intent);
在PlayerDetailActivity类中,使用它来提取数据:
PlayerDetailActivity player = getIntent().getExtras().getParcelable("player");