使用用户输入(Android app)发送的信息发送电子邮件时出现问题

时间:2017-05-10 16:21:56

标签: android

我正在构建一个小型Android应用,让人们通过电子邮件客户端捐赠食物。在用户提供基本信息(例如姓名,位置,食物类型等)之后,我似乎无法检索输入信息以将其合并到电子邮件文本内容中。为变量(来自EditText的用户输入)提供的空间保持输出“null”而不是显示用户的信息。

这是XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
    android:id="@+id/textView7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name of Company / Individual:" />

<EditText
    android:id="@+id/etCompName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: Chicken Hut, Kaldi's Coffee, etc" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Telephone Number (required)" />

<EditText
    android:id="@+id/etPhone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Ex: 0922092161"
    android:inputType="phone" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Describe your Location:" />

<EditText
    android:id="@+id/etLocation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Briefly describe where you are located..." />

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Food Type:" />

<EditText
    android:id="@+id/etFoodType"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: Local Food, Sandwiches, etc." />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Estimated Quantity (in grams):" />

<EditText
    android:id="@+id/etQuantity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:textAllCaps="true"
    android:hint="Ex: 1kg, 50g, etc." />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Snap a Picture of Food (if available)" />

<ImageView
    android:id="@+id/ivFood"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_weight="0.22" />

<Button
    android:id="@+id/bCam"
    style="@style/Widget.AppCompat.Button.Borderless.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="0dp"
    android:background="#ee9d3e"
    android:text="Open Camera"
    android:textColor="#ffffff" />

<Button
    android:id="@+id/bSubmit"
    android:background="#cecbc8"
    android:textColor="#ffffff"
    android:onClick="onButtonClickSend"
    style="@android:style/Widget.DeviceDefault.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit" />

     </LinearLayout>    

这是Java:

   package com.example.android.foodshare;

   import android.content.Intent;

   import android.os.Bundle;
   import android.support.v7.app.AppCompatActivity;
   import android.view.View;
   import android.widget.EditText;


public class Donation extends AppCompatActivity {

EditText company_name;
EditText phone_number;
EditText location;
EditText foodType;
EditText quantity;

String companyNameString;
String phoneNumberString;
String locationString;
String foodTypeString;
String quantityString;

String emailTo = "kalid.mawi@briswieth.com";
String emailSubject = "New Food Pickup Request From " + companyNameString; 
// maybe add from which company


String emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.donation);

    company_name = (EditText) findViewById(R.id.etCompName);
    phone_number = (EditText) findViewById(R.id.etPhone);
    location = (EditText) findViewById(R.id.etLocation);
    foodType = (EditText) findViewById(R.id.etFoodType);
    quantity = (EditText) findViewById(R.id.etQuantity);

}

public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}


     }

这是输出“null”作为我的错误的一部分:

*您已收到食品取件申请。请在下面找到更多详细信息:

组织名称(或个人):null

电话号码:null

位置:null

可用食物类型:null

可用数量:null *

谢谢!

2 个答案:

答案 0 :(得分:1)

在构造Activity实例时,已经将emailContent构建为字段初始化。相反,只需声明它,然后在按钮点击时初始化如下:

String emailContent;


public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();

    emailContent = "You have received a Food Pickup Request. Please find 
    further details below: \n\ n " +
    "Name of Organization (or Individual): " + companyNameString + "\n" +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {
        emailTo
    });
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}

答案 1 :(得分:1)

您正在使用空值声明初始化您的电子邮件内容,之后您不会更改它。这里:

String emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;

我建议您先声明它,然后在onButtonClicked监听器中执行上述操作:

public void onButtonClickSend(View v) {
    companyNameString = company_name.getText().toString();
    phoneNumberString = phone_number.getText().toString();
    locationString = location.getText().toString();
    foodTypeString = foodType.getText().toString();
    quantityString = quantity.getText().toString();
emailContent = "You have received a Food Pickup Request. Please find 
further details below:\n\n" +
        "Name of Organization (or Individual): " + companyNameString + "\n" 
         +
        "Telephone Number: " + phoneNumberString + "\n" +
        "Location: " + locationString + "\n" +
        "Available Food Type: " + foodTypeString + "\n" +
        "Quantity Available: " + quantityString;
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);



    //need this to prompts email client only
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Select an Email Client:"));

}
相关问题