Android 登录页面
Android Login page
我在android中写了这个小登录activity。应用程序启动时默认为第一个 activity 到 运行。但是当我尝试在 phone 上启动该应用程序时,它就崩溃了。我不知道错误来自哪里。我 运行 在 eclipse 上使用它。没有错误出现。代码有问题吗?
package com.example.ruralaid1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
String username=null,password=null;
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(username.equals("") || username.equals(null))
{
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
}
else if (pwd.equals("") || pwd.equals(null))
{
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
}
else
{
if(username.equals("admin") && password.equals("admin"))
{
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}
移动
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
在 onCreate(..)
之后 setContentView(R.layout.activity_main);
将这两行放在setContentView(R.layout.activity_main);
之后
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
代码如下所示。
public class MainActivity extends Activity {
String username=null,password=null;
AutoCompleteTextView user;
AutoCompleteTextView pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
Button b1 = (Button)findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(username.equals("") || username.equals(null))
{
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
}
else if (pwd.equals("") || pwd.equals(null))
{
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
}
else
{
if(username.equals("admin") && password.equals("admin"))
{
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}
在 setContentView() 之后设置以下代码行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
Button b1 = (Button) findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (username.equals("") || username.equals(null)) {
Toast.makeText(getBaseContext(), "Username can't be empty",
Toast.LENGTH_LONG).show();
} else if (pwd.equals("") || pwd.equals(null)) {
Toast.makeText(getBaseContext(), "Password can't be empty",
Toast.LENGTH_LONG).show();
} else {
if (username.equals("admin") && password.equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(),
Menu.class);
Toast.makeText(getBaseContext(), "Login Successful",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Username and Password don't match",
Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
代码会是这样
package com.example.ruralaid1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
String username = null, password = null;
AutoCompleteTextView user, pwd;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
b1 = (Button) findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (username.equals("") || username.equals(null)) {
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
} else if (pwd.equals("") || pwd.equals(null)) {
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
} else {
if (username.equals("admin") && password.equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}
我在android中写了这个小登录activity。应用程序启动时默认为第一个 activity 到 运行。但是当我尝试在 phone 上启动该应用程序时,它就崩溃了。我不知道错误来自哪里。我 运行 在 eclipse 上使用它。没有错误出现。代码有问题吗?
package com.example.ruralaid1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
String username=null,password=null;
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(username.equals("") || username.equals(null))
{
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
}
else if (pwd.equals("") || pwd.equals(null))
{
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
}
else
{
if(username.equals("admin") && password.equals("admin"))
{
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}
移动
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
在 onCreate(..)
之后 setContentView(R.layout.activity_main);
将这两行放在setContentView(R.layout.activity_main);
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
代码如下所示。
public class MainActivity extends Activity {
String username=null,password=null;
AutoCompleteTextView user;
AutoCompleteTextView pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
Button b1 = (Button)findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(username.equals("") || username.equals(null))
{
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
}
else if (pwd.equals("") || pwd.equals(null))
{
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
}
else
{
if(username.equals("admin") && password.equals("admin"))
{
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}
在 setContentView() 之后设置以下代码行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
AutoCompleteTextView pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
Button b1 = (Button) findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (username.equals("") || username.equals(null)) {
Toast.makeText(getBaseContext(), "Username can't be empty",
Toast.LENGTH_LONG).show();
} else if (pwd.equals("") || pwd.equals(null)) {
Toast.makeText(getBaseContext(), "Password can't be empty",
Toast.LENGTH_LONG).show();
} else {
if (username.equals("admin") && password.equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(),
Menu.class);
Toast.makeText(getBaseContext(), "Login Successful",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Username and Password don't match",
Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
代码会是这样
package com.example.ruralaid1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
String username = null, password = null;
AutoCompleteTextView user, pwd;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
pwd = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
b1 = (Button) findViewById(R.id.button1);
username = user.getText().toString();
password = pwd.getText().toString();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (username.equals("") || username.equals(null)) {
Toast.makeText(getBaseContext(), "Username can't be empty", Toast.LENGTH_LONG).show();
} else if (pwd.equals("") || pwd.equals(null)) {
Toast.makeText(getBaseContext(), "Password can't be empty", Toast.LENGTH_LONG).show();
} else {
if (username.equals("admin") && password.equals("admin")) {
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Menu.class);
Toast.makeText(getBaseContext(), "Login Successful", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Username and Password don't match", Toast.LENGTH_SHORT).show();
user.setText(null);
pwd.setText(null);
}
}
}
});
}
}