我正在编写一个Android应用程序,一旦点击登录按钮就会检查用户的用户名和密码
我在模拟器中运行应用程序,当我单击登录按钮时,应用程序崩溃
package com.google.android.InitialScreen;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
public class InitialScreen extends TabActivity implements View.OnClickListener {
EditText username;
EditText password;
Button login;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
username = (EditText) findViewById(R.id.usernamefield);
password = (EditText) findViewById(R.id.passwordfield);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 1")
.setContent(R.id.tabview1));
this.login = (Button)findViewById(R.id.login);
this.login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((username.getText().length()==0)&&(password.getText().length()==0))
username.setText("u&p");
// Toast.makeText(getApplicationContext(), "Please Enter Username and Password", Toast.LENGTH_SHORT).show();
else if ((username.getText().length()==0)){
username.setText("u");
// Toast.makeText(getApplicationContext(), "Please Enter Username", Toast.LENGTH_SHORT).show();
}
else if (password.getText().length()==0)
username.setText("p");
else
username.setText("ok");
}
});
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator("TAB 2")
.setContent(R.id.tabview2));
mTabHost.setCurrentTab(0);
}
}
答案 0 :(得分:1)
我猜你应该在调用
后初始化用户名和密码super.onCreate(savedInstanceState);
setContentView(R.layout.main);
只是一个猜测。发布堆栈跟踪 - 这可能会有所帮助。