无法启动活动组件信息{com.android.loginapp/com.android.loginapp.login}
此错误导致我的应用程序每次运行时都会停止,并且无法找出原因。认为这可能是XML文件中的一个问题,但看不到它 上面说,无法启动活动组件信息{com.android.loginapp/com.android.loginapp.login},android,Android,此错误导致我的应用程序每次运行时都会停止,并且无法找出原因。认为这可能是XML文件中的一个问题,但看不到它 上面说,不幸的是,登录应用程序已停止工作 代码如下: login.java public class login extends Activity { public static final String MY_PREFS = "SharedPreferences"; private DatabaseAdapter dbHelper; private EditText theUserna
不幸的是,登录应用程序已停止工作
代码如下:
login.java
public class login extends Activity {
public static final String MY_PREFS = "SharedPreferences";
private DatabaseAdapter dbHelper;
private EditText theUsername;
private EditText thePassword;
private Button loginButton;
private Button registerButton;
private Button clearButton;
private Button exitButton;
private CheckBox rememberDetails;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, 0);
Editor editor = mySharedPreferences.edit();
editor.putLong("uid", 0);
editor.commit();
dbHelper = new DatabaseAdapter(this);
dbHelper.open();
setContentView(R.layout.main);
initControls();
}
private void initControls() {
//Set the activity layout.
theUsername = (EditText) findViewById(R.id.Username);
thePassword = (EditText) findViewById(R.id.Password);
loginButton = (Button) findViewById(R.id.Login);
registerButton = (Button) findViewById(R.id.Register);
//clearButton = (Button) findViewById(R.id.Clear);
//exitButton = (Button) findViewById(R.id.Exit);
rememberDetails = (CheckBox) findViewById(R.id.RememberMe);
//Create touch listeners for all buttons.
loginButton.setOnClickListener(new Button.OnClickListener(){
public void onClick (View v){
LogMeIn(v);
}
});
registerButton.setOnClickListener(new Button.OnClickListener(){
public void onClick (View v){
Register(v);
}
});
clearButton.setOnClickListener(new Button.OnClickListener(){
public void onClick (View v){
ClearForm();
}
});
exitButton.setOnClickListener(new Button.OnClickListener(){
public void onClick (View v){
Exit();
}
});
//Create remember password check box listener.
rememberDetails.setOnClickListener(new CheckBox.OnClickListener(){
public void onClick (View v){
RememberMe();
}
});
//Handle remember password preferences.
SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
String thisUsername = prefs.getString("username", "");
String thisPassword = prefs.getString("password", "");
boolean thisRemember = prefs.getBoolean("remember", false);
if(thisRemember) {
theUsername.setText(thisUsername);
thePassword.setText(thisPassword);
rememberDetails.setChecked(thisRemember);
}
}
/**
* Deals with Exit option - exits the application.
*/
private void Exit()
{
finish();
}
/**
* Clears the login form.
*/
private void ClearForm() {
saveLoggedInUId(0,"","");
theUsername.setText("");
thePassword.setText("");
}
/**
* Handles the remember password option.
*/
private void RememberMe() {
boolean thisRemember = rememberDetails.isChecked();
SharedPreferences prefs = getSharedPreferences(MY_PREFS, 0);
Editor editor = prefs.edit();
editor.putBoolean("remember", thisRemember);
editor.commit();
}
/**
* This method handles the user login process.
* @param v
*/
private void LogMeIn(View v) {
//Get the username and password
String thisUsername = theUsername.getText().toString();
String thisPassword = thePassword.getText().toString();
//Assign the hash to the password
thisPassword = md5(thisPassword);
// Check the existing user name and password database
Cursor theUser = dbHelper.fetchUser(thisUsername, thisPassword);
if (theUser != null) {
startManagingCursor(theUser);
if (theUser.getCount() > 0) {
saveLoggedInUId(theUser.getLong(theUser.getColumnIndex(DatabaseAdapter.COL_ID)), thisUsername, thePassword.getText().toString());
stopManagingCursor(theUser);
theUser.close();
Intent i = new Intent(v.getContext(), Helloworld.class);
startActivity(i);
}
//Returns appropriate message if no match is made
else {
Toast.makeText(getApplicationContext(),
"You have entered an incorrect username or password.",
Toast.LENGTH_SHORT).show();
saveLoggedInUId(0, "", "");
}
stopManagingCursor(theUser);
theUser.close();
}
else {
Toast.makeText(getApplicationContext(),
"Database query error",
Toast.LENGTH_SHORT).show();
}
}
/**
* Open the Registration activity.
* @param v
*/
private void Register(View v)
{
Intent i = new Intent(v.getContext(), Register.class);
startActivity(i);
}
private void saveLoggedInUId(long id, String username, String password) {
SharedPreferences settings = getSharedPreferences(MY_PREFS, 0);
Editor myEditor = settings.edit();
myEditor.putLong("uid", id);
myEditor.putString("username", username);
myEditor.putString("password", password);
boolean rememberThis = rememberDetails.isChecked();
myEditor.putBoolean("rememberThis", rememberThis);
myEditor.commit();
}
/**
* Deals with the password encryption.
* @param s The password.
* @return
*/
private String md5(String s) {
try {
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0; i<messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
}
catch (NoSuchAlgorithmException e) {
return s;
}
}
}
公共类登录扩展活动{
公共静态最终字符串MY\u PREFS=“SharedReferences”;
专用数据库适配器dbHelper;
私有编辑文本用户名;
私人编辑文本密码;
私人按钮登录按钮;
专用按钮寄存器按钮;
私人按钮clearButton;
私人按钮退出按钮;
私人复选框记忆详情;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedPreferences mySharedPreferences=getSharedPreferences(我的首选项,0);
Editor Editor=mySharedPreferences.edit();
编辑器.putLong(“uid”,0);
commit();
dbHelper=新数据库适配器(此);
dbHelper.open();
setContentView(R.layout.main);
initControls();
}
私有控件(){
//设置活动布局。
用户名=(编辑文本)findViewById(R.id.Username);
密码=(编辑文本)findViewById(R.id.Password);
loginButton=(按钮)findViewById(R.id.Login);
registerButton=(按钮)findViewById(R.id.Register);
//clearButton=(按钮)findViewById(R.id.Clear);
//exitButton=(按钮)findViewById(R.id.Exit);
rememberDetails=(复选框)findViewById(R.id.RememberMe);
//为所有按钮创建触摸侦听器。
loginButton.setOnClickListener(新建按钮.OnClickListener(){
公共void onClick(视图v){
洛美(v);
}
});
registerButton.setOnClickListener(新建Button.OnClickListener(){
公共void onClick(视图v){
登记册(五);
}
});
clearButton.setOnClickListener(新建Button.OnClickListener(){
公共void onClick(视图v){
ClearForm();
}
});
exitButton.setOnClickListener(新建Button.OnClickListener(){
公共void onClick(视图v){
退出();
}
});
//创建记住密码复选框侦听器。
rememberDetails.setOnClickListener(新建复选框.OnClickListener(){
公共void onClick(视图v){
记住();
}
});
//处理并记住密码首选项。
SharedReferences prefs=GetSharedReferences(MY_prefs,0);
String thisUsername=prefs.getString(“用户名”,“参数”);
String thisPassword=prefs.getString(“密码”,“密码”);
boolean thisremble=prefs.getBoolean(“记住”,false);
如果(记住){
username.setText(此用户名);
密码.setText(此密码);
rememberDetails.setChecked(thisremberd);
}
}
/**
*处理退出选项-退出应用程序。
*/
私有无效退出()
{
完成();
}
/**
*清除登录表单。
*/
私有void ClearForm(){
SaveLoggedUID(0,“”,“”);
username.setText(“”);
password.setText(“”);
}
/**
*处理“记住密码”选项。
*/
私人无效记忆(){
布尔thisRemember=rememberDetails.isChecked();
SharedReferences prefs=GetSharedReferences(MY_prefs,0);
编辑器编辑器=prefs.edit();
putBoolean(“记住”,thisleeve);
commit();
}
/**
*此方法处理用户登录过程。
*@param v
*/
私有无效日志(视图五){
//获取用户名和密码
String thisUsername=theUsername.getText().toString();
字符串thisPassword=thePassword.getText().toString();
//将哈希分配给密码
thisPassword=md5(thisPassword);
//检查现有的用户名和密码数据库
游标theUser=dbHelper.fetchUser(thiusername,thipassword);
if(theUser!=null){
启动管理光标(用户);
if(theUser.getCount()>0){
saveLoggedInUId(user.getLong(user.getColumnIndex(DatabaseAdapter.COL_ID)),这个用户名,password.getText().toString();
停止管理光标(用户);
theUser.close();
Intent i=新的Intent(v.getContext(),Helloworld.class);
星触觉(i);
}
//如果不匹配,则返回相应的消息
否则{
Toast.makeText(getApplicationContext(),
“您输入的用户名或密码不正确。”,
吐司。长度(短)。show();
SaveLoggedUID(0,“”,“”);
}
停止管理光标(用户);
theUser.close();
}
否则{
Toast.makeText(getApplicationContext(),
“数据库查询错误”,
吐司。长度(短)。show();
}
}
/**
*打开注册活动。
*@param v
*/
专用无效登记簿(视图五)
{
Intent i=新的Intent(v.getContext(),Register.class);
星触觉(i);
}
私有void saveLoggedInUId(长id、字符串用户名、字符串密码){
SharedReferences设置=获取SharedReferences(我的首选项,0);
Editor myEditor=settings.edit();
myEditor.putLong(“uid”,id);
putString(“用户名”,用户名);
putString(“密码”,password);
boolean rememberThis=rememberDetails.isChecked();
putBoolean(“回忆”,回忆);
myEditor.commit();
}
/**
*处理密码加密。
*@param是密码。
*@返回
*/
私有字符串md5(字符串s){
试一试{
MessageDigest=java.security.MessageDigest.getInstance(“MD5”);
更新(s.getBytes());
字节messageDigest[]=digest.digest();
StringBuffer hexString=新的StringBuffer();
对于(int i=0;i您已经注释掉了初始化清除和退出按钮引用的代码,但仍然对这些null
按钮引用调用setOnClickListener()
。<
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6" >
<TextView
android:id="@+id/Title"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#46C1EF"
android:gravity="center|fill_vertical"
android:text="MemRar"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="@+id/lblLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="51dp"
android:layout_marginTop="71dp"
android:text="Login:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#223355" />
<EditText
android:id="@+id/Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/lblLogin"
android:layout_below="@+id/lblLogin"
android:layout_marginTop="23dp"
android:ems="10"
android:gravity="fill_horizontal"
android:hint="Username"
android:inputType="textPersonName"
android:lines="1"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Username"
android:layout_alignRight="@+id/Username"
android:layout_below="@+id/Username"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:lines="1"
android:selectAllOnFocus="true" />
<Button
android:id="@+id/Login"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_below="@+id/RememberMe"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:background="#46C1EF"
android:textColor="#ffffff"
android:clickable="true"
android:gravity="center_vertical|center_horizontal"
android:text="@string/Login" />
<Button
android:id="@+id/Register"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/Login"
android:background="#46C1EF"
android:textColor="#ffffff"
android:clickable="true"
android:gravity="center_vertical|center_horizontal"
android:text="@string/Register" />
<CheckBox
android:id="@+id/RememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Password"
android:layout_below="@+id/Password"
android:textColor="#223355"
android:text="Remember Me" />
</RelativeLayout>
//clearButton = (Button) findViewById(R.id.Clear);
//exitButton = (Button) findViewById(R.id.Exit);