-1

I believe that I have stated the variables at the top of the code under EditText however i am still getting this error message would anyone be able to take a look and maybe find a solution. by no means am i an expert at code im just trying to pass my uni module :D

package com.example.richard.bradfordcoursefinder;

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

public class Main4Activity extends AppCompatActivity {


EditText email, name; 
Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    email = (EditText) findViewById(R.id.email);
    name = (EditText) findViewById(R.id.name);

    send = (Button) findViewById(R.id.send);

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String email = email.getText().toString();
            String name = name.getText().toString();

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{name});
            intent.putExtra(Intent.EXTRA_SUBJECT, email);

            intent.setType("message/rfc822");

            startActivity(Intent.createChooser(intent, "Select Email App" 
 ));
        }
    });


    }
 }
  • 1
    `String email` [shadows](http://stackoverflow.com/questions/1092099/what-is-variable-shadowing-used-for-in-a-java-class) `this.email` and `email.getText()` references that inner definition. Hence the error message. Same for `name`. Either reference the fields using `this.email` or --preferred-- get rid of the shadowing. – dhke Apr 21 '17 at 20:38

2 Answers2

0

Change the variable name. dont use same name for different

change either

EditText email to txtEmail 

or

String email to emailId;
Ajay Shrestha
  • 2,433
  • 1
  • 21
  • 25
0

Here is your solution:

Use Intent.ACTION_SENDTO, Use different names.And setData for mailto.

Here I added these in your code.

public class Main4Activity extends AppCompatActivity {


EditText etEmail, etName; 
Button btSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);

    etEmail = (EditText) findViewById(R.id.email);
    etName = (EditText) findViewById(R.id.name);

    btSend = (Button) findViewById(R.id.send);

    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String email = etEmail.getText().toString();
            String name = etName.getText().toString();

            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("mailto:"));
            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{name});
            intent.putExtra(Intent.EXTRA_SUBJECT, email);
            intent.setType("message/rfc822");
            startActivity(Intent.createChooser(intent, "Select Email App" 
 ));
        }
    });


    }
 }
Eren Utku
  • 1,731
  • 1
  • 18
  • 27