1

I need an Array with username and password. The method containing the array needs to return it. I tried the following code but android studio is showing and error when I tried to access the array from another method:

public class MainActivity extends Activity {

    private static Boolean log_status;

    public  String[] credentials () {
        String Username;
        String Password;
        String login_data;
        EditText editText = (EditText) findViewById(R.id.username);
        EditText editPass = (EditText) findViewById(R.id.password);
        Username = editText.getText().toString();
        Password = editPass.getText().toString();

        String[] log_data= new  String[1];
        log_data[0]= Username;
        log_data[1]=Password;

        return log_data;

    }   

How can I make the log_data array be accessed by another method? Does my code return an array?

1
  • Where is your onCreate? And why create login_data while not using it? Commented Aug 22, 2015 at 19:31

4 Answers 4

1

You made a few mistakes, please try this.

EditText password,username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        password = (EditText)findViewById(R.id.passWord);
        username = (EditText)findViewById(R.id.userName);
    }
    public  String[] LogInfo () {
        String passWord = password.getText().toString();
        String userName = username.getText().toString();

            String[] log_data= new String[2];
            log_data[0] = passWord;
            log_data[1] = userName;

            return log_data;

        }
Sign up to request clarification or add additional context in comments.

Comments

0

How can you access first element of array which is limited to 1

  String[] log_data= new  String[1];
  log_data[0]= Username;
  log_data[1]=Password;

Increase the size of the array to 2

  String[] log_data= new  String[2];
  log_data[0]= Username;
  log_data[1]=Password;

Comments

0
String[] log_data= new  String[1];
log_data[0]= Username;
log_data[1]=Password;

Your array size is just 1, not 2.

Try changing

String[] log_data= new  String[2];

Also, if it doesn't solve, can you tell exact error message you are getting ?

Comments

0

String[] log_data= new String[1]; creates a Vector with 1 dimension, so I think it should be: String[] log_data= new String[2];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.