Intent Object显示null将数据发送到其他活动

时间:2018-02-12 13:18:19

标签: java android

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

假设我有两项活动:

  1. 个人信息

  2. 文献信息。

  3. 我在做什么:当用户填写所有给定的编辑文本字段并点击保存时,我将所有数据通过意图发送到第3个活动,现在用户处于文档活动中,输入数据并点击保存,数据被传输到第3次活动。

    问题在于第3次活动:我只根据最新活动获取数据 - 我的意思是如果用户首先输入个人信息然后输入文档信息,我只会获得数据显示双重活动和副数据反之亦然。

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

    个人信息活动:

    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);
    }
    

    文档信息活动

    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);
        }
    

    第3次活动

        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();
    }
    

2 个答案:

答案 0 :(得分:1)

您无法使用FirstActivity将数据发送至ThirdActivityintent,您可以再次通过意图将数据发送至FirstActivitySecondActivity您必须在SecondActivity上获取数据,然后再次将数据发送到ThirdActivity,否则您将获得空数据。

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

参考代码:

public class Constants {

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

  }

当您通过Edittext获取用户数据时,获取并设置为常量类中的POJO,以及您希望在何处获取创建引用并获取数据。

解决方案2 :您有8项活动,并且您希望保存所有信息并从 MainActivity.java

创建PDF文件

以下是解决方案:当您想要getData时,使用SharedPreference保存数据,保存所有不同活动中的数据以及 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", "");

答案 1 :(得分:0)

您只需发送包含每个活动中所有数据的pojo即可解决问题。您只需要将pojo从一个活动传递到另一个活动。

你需要创建这样的东西:

public class Info implements Serializable {

  private String firstName;
  private String born;
  private String nationality;
  //etc ...

  // Data from another activity:
  private String passportNumber;
  private String idCard;
  private String license;
  //etc ...

  // Data from another activity:
  // etc ...

  // Constructor

  // Setter and Getter
}

然后您可以使用以下方式发送数据意图:

public void getting_data_from_fields_and_sending_on_click_listner () {
  Intent sendingData = new Intent (Personal_Information_Activity.this , Selecting_Activity.class);
  Info info = new Info();
  info.setFirstName(fullName.getText().toString());
  info.setBorn(born.getText().toString());
  info.setNationality(nationality.getText().toString());
  // etc

  // Then attach the data to intent.
  sendingData.putExtra("info" , info);

  startActivity(sendingData);
}

然后,您可以通过以下方式获取另一项活动中的数据信息:

private Info mInfo;

@Override
public void onCreate(Bundle savedInstanceState) {
  ...
  Intent intent = getIntent();
  mInfo = (Info) intent.getSerializableExtra("info");
  ...
}

再次将数据设置为信息 pojo:

private void getting_data_from_fields_and_sending_on_click_listner() {

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

  mInfo.setPassportNumber(passportNumber.getText().toString());
  mInfo.setIdCard(idCardNumber.getText().toString());
  mInfo.setLicense(licenceNumebr.getText().toString());
  // etc ...

  sendingData.putExtra("info" , mInfo);
  startActivity(sendingData);
}

使用上述所有代码,您可以避免头痛,以便记住所有额外键。您还需要为所有活动维护一个对象,即Info对象。