我的EditText一直显示为null

时间:2017-07-15 18:52:15

标签: android android-intent

我一直致力于这个收集信息的应用程序,然后以电子邮件形式发送。我的所有其他EditTexts都在使用第一个,例如pilot(提示是名称,如在飞行员的名字中)。我已经彻底地完成了这个问题多个小时,但我似乎无法找到问题所在。我知道它的null的唯一原因是因为当它进入电子邮件格式时,所有它都是空的

public class InfoSheet extends AppCompatActivity {
private double VesselUnits;
private EditText pilot, ship, to, from, LOA, MBDTH, CUSD, zone1, zone2, CallSign;
private Spinner agent_spinner;
private Button btnSubmit;
private String date, agent, Spilot, Sship, Sto, Sfrom, Szone1, Szone2, SCallSign, SVesselUnits;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info_sheet);
    date=getTodaysDate();
    addListenerOnButton();
}
public void collectNCalc(){
    //grab all of our info
    agent_spinner = (Spinner) findViewById(R.id.agent_spinner);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    pilot = (EditText) findViewById(R.id.Pilot);
    ship = (EditText) findViewById(R.id.ship);
    to = (EditText) findViewById(R.id.to);
    from = (EditText) findViewById(R.id.from);
    LOA = (EditText) findViewById(R.id.LOA);
    MBDTH = (EditText) findViewById(R.id.MBDTH);
    CUSD = (EditText) findViewById(R.id.CUSD);
    zone1 = (EditText) findViewById(R.id.zone1);
    zone2 = (EditText) findViewById(R.id.zone2);
    CallSign = (EditText) findViewById(R.id.CallSign);

    //convert what we need to int to do equations
    String sLOA = LOA.getText().toString();
    double intLOA = Integer.valueOf(sLOA);
    intLOA = intLOA*3.281;

    String sMBDTH = MBDTH.getText().toString();
    double intMBDTH = Integer.valueOf(sMBDTH);
    intMBDTH = intMBDTH*3.281;

    String sCUSD = CUSD.getText().toString();
    double intCUSD = Integer.valueOf(sCUSD);
    intCUSD = intCUSD*3.281;

    VesselUnits = intLOA*intMBDTH*intCUSD;
    VesselUnits = VesselUnits/10000;
    Spilot=pilot.getText().toString();
    Sship=ship.getText().toString();
    Sto=to.getText().toString();
    Sfrom=from.getText().toString();
    Szone1=zone1.getText().toString();
    Szone2=zone2.getText().toString();
    SCallSign=CallSign.getText().toString();
    agent=agent_spinner.getSelectedItem().toString();
    //SVesselUnits=String.valueOf(VesselUnits);
}
public void addListenerOnButton() {
    final Context context2 = this;
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i2 = new Intent(context2, DigitalSignature.class);
            //do the Calc
            collectNCalc();
            //pass to next activity
            i2.putExtra("Pilot",Spilot);
            i2.putExtra("ship",Sship);
            i2.putExtra("to",Sto);
            i2.putExtra("from",Sfrom);
            i2.putExtra("zone1",Szone1);
            i2.putExtra("zone2",Szone2);
            i2.putExtra("callsign",SCallSign);
            i2.putExtra("agent",agent);
            i2.putExtra("vessleunits",VesselUnits);
            i2.putExtra("date",date);
            startActivity(i2);
        }

    });
}

将其发送到下一个也是最后一个活动:

public class DigitalSignature extends AppCompatActivity {
String pilot, ship, to, from, zone1, zone2, CallSign, agent, date;
Toolbar toolbar;
Button btn_get_sign, mClear, mGetSign, mCancel, btn_send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_digital_signature);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        pilot = extras.getString("pilot");
        ship = extras.getString("ship");
        to = extras.getString("to");
        from = extras.getString("from");
        zone1 = extras.getString("zone1");
        zone2 = extras.getString("zone2");
        CallSign = extras.getString("callsign");
        agent = extras.getString("agent");
        vesselUnit = extras.getDouble("vesselunits");
        date = extras.getString("date");
    }
btn_send.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            Uri path = Uri.parse("file://" + file);
            Intent emailIntent = new Intent(Intent.ACTION_SEND);
            emailIntent.putExtra(Intent.EXTRA_TEXT, pilot+"\n"+ship+"\n"+to+"\n"+from+"\n"+zone1+"\n"+zone2+"\n"+CallSign+"\n"+agent+"\n"+vesselUnit);
            // set the type to 'email'
            emailIntent.setType("image/png");
            String to[] = {"metropilottickets@gmail.com"};
            emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
            // the attachment
            emailIntent.putExtra(Intent.EXTRA_STREAM, path);
            // the mail subject
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, pilot+"'s Ticket for "+ship);
            startActivity(Intent.createChooser(emailIntent , "Send email..."));
        }
    });
}

我遗漏了许多与我的问题无关的其他代码,但如果有人能指出我为什么会失效,那么你就会成为救命恩人!

1 个答案:

答案 0 :(得分:2)

您需要使用extras.getString("Pilot");

代替extras.getString("pilot");