in this simple Application of my first program in Android i want to get username
and password
from User. but after click on button that return NULL
and thats incorrect, username
and password
fields have string and are not NULL
.
Textusername
and Textpassword
in this code are NULL
and can not get string from R.id.username
AND R.id.password
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="@string/EnterUsername"
/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:id="@+id/username"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="@string/EnterPassword"
/>
<EditText
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:id="@+id/password"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/submitButton"
android:text="@string/SubmitButton"
android:gravity="center"
android:background="@drawable/purple"/>
</LinearLayout>
My Code:
package com.example.AndroidMultiPage;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
Button submit = (Button) findViewById(R.id.submitButton);
final String Textusername = username.getText().toString();
final String Textpassword = password.getText().toString();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(
MyActivity.this, Textusername,
Toast.LENGTH_SHORT).show();
//TOAST SHOW NULL
}
});
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…