我正在尝试将对象从一个 Activity 传递到另一个 Activity 。我尝试过仅使用 Intent 以及如何使用 bundle ,但我不确定出了什么问题。我在这里查看了类似的解决方案,这就是我获得大部分代码的地方,但似乎我的复制和粘贴不起作用。
这是我的主课
public class MainActivity extends AppCompatActivity {
Item item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewItem();
Button buttonOne = findViewById(R.id.itemButton);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ViewItemDetails.class);
Bundle bundle = new Bundle();
bundle.putSerializable("item", item);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
void createNewItem(){
item=new Item("Pixel 4","https://google.com",1000.00);
}
}
这是我想要参加的 Activity :
public class ViewItemDetails extends AppCompatActivity {
Intent intent= getIntent();
Bundle bundle= intent.getExtras();
Item item = (Item) bundle.getSerializable("item");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_details);
setStrings();
setButtons();
}
void setStrings() {
try {
TextView nameTextView = findViewById(R.id.itemNameid);
nameTextView.setText(item.getItemName());
TextView itemInitTV = findViewById(R.id.initalPriceNumID);
itemInitTV.setText(Double.toString(item.getInitPrice()));
TextView itemCurrTV = findViewById(R.id.currentPriceNumid);
itemCurrTV.setText(Double.toString(item.getCurrentPrice()));
}catch (NullPointerException e){
//do noting
}
}
void setButtons(){
Button buyButton = findViewById(R.id.buyButtonid);
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uriUrl = Uri.parse(item.getWebaddress());
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
});
Button refreshButton= findViewById(R.id.refrechId);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Double newPrice= new GenerateNewPrice().GenerateNewPrice(item.getWebaddress());
Toast toast = Toast.makeText(getApplicationContext(),Double.toString(newPrice), Toast.LENGTH_SHORT);
toast.show();
}
});
Button editButton= findViewById(R.id.editid);
editButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(),"Attempt to edit", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
这是我尝试在 Activity 之间传递的对象。
public class Item implements Serializable {
String itemName, webaddress;
Double initPrice, CurrentPrice;
public Item(String itemName, String webaddress, Double initPrice) {
this.itemName = itemName;
this.webaddress = webaddress;
this.initPrice = initPrice;
}
public String getItemName() {
return itemName;
}
public String getWebaddress() {
return webaddress;
}
public Double getInitPrice() {
return initPrice;
}
public Double getCurrentPrice() {
return CurrentPrice;
}
}
当我在手机上运行该应用程序时,我单击按钮,然后应用程序关闭。
感谢您的帮助。如果需要,我可以添加更多代码。我在这里看到了类似的问题,但它们对我不起作用。我从这些帖子中得到了类似的代码,但还没有解决我的解决方案。 我感谢您提供的任何反馈。 感谢您抽出时间。
最佳答案
无论是现在还是将来有关 SOF 的帮助,请记住,错误日志在这种情况下总是有帮助的。
不过,这里有一些要点..
- 您应该遵循 Activity 生命周期规则,在
onCreate()
中获取数据将是一个好主意。 - 您应该使用 Parcelable 而不是 Serialized。效率要高得多。
关于java - 如何将对象传递给android studio中的新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369864/