gpt4 book ai didi

java - 向其他 Activity 发送数据后,Intent 对象显示 null

转载 作者:行者123 更新时间:2023-12-02 11:38:28 25 4
gpt4 key购买 nike

我在通过 Intent 发送数据时遇到问题。

假设我有两项 Activity :

  1. 个人信息

  2. 文档信息。

我正在做的事情:当用户填写所有给定的编辑文本字段并单击“保存”时,我通过 Intent 将所有数据发送到第三个 Activity ,现在用户处于文档 Activity 中,输入数据并单击“保存”,并且数据被传输到第三个 Activity 。

问题出在第三个 Activity 中:我只获取基于最新 Activity 的数据 - 我的意思是,如果用户首先输入个人信息,然后输入文档信息,我只会获取文档 Activity 的数据和副 Activity 反之亦然。

我正在构建一个应用程序,可以从用户那里获取数据并将其显示在 pdf 文件中

个人信息 Activity :

public void getting_data_from_fields_and_sending_on_click_listner () {

radio_Group_Martial_Status.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

RadioButton radioButton = (RadioButton) findViewById(checkedId);
martial_status = radioButton.getText().toString() ;

}
});

Intent sendingData = new Intent (Personal_Information_Activity.this , Selecting_Activity.class);
sendingData.putExtra("FirstName" , fullName.getText().toString() );
sendingData.putExtra("Born" , born.getText().toString() );
sendingData.putExtra("Nationality" , nationality.getText().toString() );
sendingData.putExtra("CurrentPlace" , currentPlace.getText().toString() );
sendingData.putExtra("Address" , address.getText().toString() );
sendingData.putExtra("PhoneNumber" , phoneNumber.getText().toString() );
sendingData.putExtra("EmailAddress" , emailAddress.getText().toString() );
sendingData.putExtra("SelfIntro" , selfIntro.getText().toString() );
sendingData.putExtra("Day" , day.getSelectedItem().toString());
sendingData.putExtra("Month" , month.getSelectedItem().toString() );
sendingData.putExtra("Year" , year.getSelectedItem().toString() );
sendingData.putExtra("MartialStatus" , martial_status);

startActivity(sendingData);
}

文档信息 Activity

private void getting_data_from_fields_and_sending_on_click_listner() {

Intent sendingData = new Intent (Documentation_Activity.this , Selecting_Activity.class);

String passportIssueDate = passportIssueM.getSelectedItem().toString()+"-"+passportIssueY.getSelectedItem().toString();
String idCardIssueDate = idIssueM.getSelectedItem().toString()+"-"+idIssueY.getSelectedItem().toString();
String licenceIssueDate = licenceIssueM.getSelectedItem().toString()+"-"+licenceIssueY.getSelectedItem().toString();
String passportExpiryDate = passportExpiryM.getSelectedItem().toString()+"-"+passportExpiryY.getSelectedItem().toString();
String idCardExpiryDate = idExpiryM.getSelectedItem().toString()+"-"+idExpiryY.getSelectedItem().toString();
String licenceExpiryDate = licenceExpiryM.getSelectedItem().toString()+"-"+licenceExpiryY.getSelectedItem().toString();

sendingData.putExtra("PassportNumeber" , passportNumber.getText().toString());
sendingData.putExtra("IdCard" , idCardNumber.getText().toString());
sendingData.putExtra("Licence" , licenceNumebr.getText().toString());

sendingData.putExtra("PID" , passportIssueDate);
sendingData.putExtra("PED" , passportExpiryDate);
sendingData.putExtra("IID" , idCardIssueDate);
sendingData.putExtra("IED" , idCardExpiryDate);
sendingData.putExtra("LID" , licenceIssueDate);
sendingData.putExtra("LED" , licenceExpiryDate);

startActivity(sendingData);
}

第三次 Activity

    public void gettingInfoFromTheDocumentationalInformationActivity() {    

passportNumber = getInformation.getStringExtra("PassportNumeber");
passportIssue = getInformation.getStringExtra("PID");
passportExpiry = getInformation.getStringExtra("PED");
idNumebr = getInformation.getStringExtra("IdCard");
idIssue = getInformation.getStringExtra("IID");
idExpiry = getInformation.getStringExtra("IED");
licenceNumebr = getInformation.getStringExtra("Licence");
licenceIssue = getInformation.getStringExtra("LID");
licenceExpiry = getInformation.getStringExtra("LED");
}

public void gettingInfoFromThePersonalInformationActivity() {

fullName = getInformation.getStringExtra("FirstName");
born = getInformation.getStringExtra("Born");
nationality = getInformation.getStringExtra("Nationality");
currentLocation = getInformation.getStringExtra("CurrentPlace");
address = getInformation.getStringExtra("Address");
emailAddress = getInformation.getStringExtra("EmailAddress");
selfInfo = getInformation.getStringExtra("SelfIntro");
phoneNumber = getInformation.getStringExtra("PhoneNumber");
day = getInformation.getStringExtra("Day");
month = getInformation.getStringExtra("Month");
year = getInformation.getStringExtra("Year");
martialStatus = getInformation.getStringExtra("MartialStatus");
}

private void createPdf() throws FileNotFoundException, DocumentException
{
Document document = new Document(PageSize.A4);

String output = Environment.getExternalStorageDirectory()+"/mypdf.pdf";

PdfWriter.getInstance(document, new FileOutputStream(output));

document.open();

document.add(new Paragraph("Name : " +fullName));
document.add(new Paragraph("Date of Birth : "+ day+"-"+month+"-"+year));
document.add(new Paragraph("Birth Place : " +born));
document.add(new Paragraph("Nationality : " + nationality));
document.add(new Paragraph("Current Place : " +currentLocation));
document.add(new Paragraph("Martial Status : " + martialStatus));
document.add(new Paragraph("Address : " + address));
document.add(new Paragraph("Phone Number : " +phoneNumber));
document.add(new Paragraph("Email : " + emailAddress));
document.add(new Paragraph("Self Introduction : " + selfInfo));
document.add(new Paragraph("__________________________________"));

document.add(new Paragraph("Passport Number : " + passportNumber));
document.add(new Paragraph("Issue Date : " +passportIssue));
document.add(new Paragraph("Expiry Date : " + passportExpiry));
document.add(new Paragraph("ID Card Number : " + idNumebr));
document.add(new Paragraph("Issue Date : " +idIssue));
document.add(new Paragraph("Expiry Date : " + idExpiry));
document.add(new Paragraph("Licence Number : " + licenceNumebr));
document.add(new Paragraph("Issue Date : " +licenceIssue));
document.add(new Paragraph("Expiry Date : " + licenceExpiry));

document.close();
}

最佳答案

您无法使用intent将数据从FirstActivity发送到ThirdActivity,您可以将数据发送到FirstActivity > 再次按 Intent 发送到 SecondActivity ,您必须获取 SecondActivity 上的数据,并且再次必须将数据发送到 ThirdActivity 否则您将获得 null 数据。

解决方案 1:您可以创建一个 Constant 类并在常量类上存储数据,然后从 Constant 类中获取数据或者创建一个 POJO 类并将 Data 设置为 POJO并使其可序列化,将 POJO 类发送到您的 Activity 的下一个。

引用代码:

public class Constants {

public static ArrayList<LoadDataResult> data = new ArrayList<LoadDataResult>();

}

当您通过 Edittext get 从 User 获取数据并将其设置为 Constant 类中的 POJO 时,以及您想要在哪里获取创建引用并获取数据。

解决方案 2:您有 8 个 Activity ,并且想要保存所有信息并从 MainActivity.java 创建 PDF 文件

这是解决方案:使用SharedPreference来保存数据,保存来自所有不同 Activity 的数据,并在您的MainActivity中保存数据只需要从 SharePreference 获取数据,通过使用它也不会丢失数据。

FirstActivity.java:

 SharedPreferences pref;
SharedPreferences.Editor editor;

pref = (this.getApplicationContext()).getSharedPreferences("USER_DATA", MODE_PRIVATE);

editor = pref.edit();
editor.putBoolean("login_status", true);
editor.putString("CODE", "" + String.valueOf(obj.get("code")));
editor.putString("NAME", "" + String.valueOf(obj.get("message")));
editor.putString("USER_ID", "" + String.valueOf(obj.get("user id")));
editor.commit();

SecondActivity.java:

     SharedPreferences pref;
SharedPreferences.Editor editor;

pref = (this.getApplicationContext()).getSharedPreferences("USER_DATA", MODE_PRIVATE);



editor.putString("ADDRESS", "" + String.valueOf(obj.get("message")));
editor.putString("PHONE", "" + String.valueOf(obj.get("user id")));
editor.commit();

MainActivity.java :当你想获取所有数据时

SharedPreferences pref;
SharedPreferences.Editor editor;

pref = (this.getApplicationContext()).getSharedPreferences("USER_DATA", MODE_PRIVATE);

String name = pref.getString("NAME", "");
String userId = pref.getString("USER_ID", "");
String address = pref.getString("ADDRESS", "");
String phone= pref.getString("PHONE", "");

关于java - 向其他 Activity 发送数据后,Intent 对象显示 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com