2017-05-10 7 views
0

私は、電子メールクライアントから食品を寄付できる小さな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 = "[email protected]"; 
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 *

ありがとうございます!

答えて

1

アクティビティのインスタンスを作成するときにフィールド初期化として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

あなたがnull値で宣言でメールの内容を初期化していて、後でそれを変更されていません。ここで:

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:")); 

} 
+0

グレート!もしかすると別の質問。電子メールが送信された後で、(Submitを押すと)別のアクティビティに移動するにはどうしたらいいですか? OnActivityResultは私のために働いていません。 – Kalid

+0

OnActivityResultメソッドとstartActivityResultメソッドの両方のコードを表示できますか? –

関連する問題