Android 安卓eclipse+sqlite;“登录问题”;
我在安卓eclipse中创建了一个安卓应用程序项目,但登录命令有问题。 我已经注册,但无法登录,用户名和密码已使用sqlite浏览器在数据库中注册。但是当我输入用户名和密码时,我无法登录。我不知道错误在哪里。请帮忙 下面是我的mainactivity.java代码:Android 安卓eclipse+sqlite;“登录问题”;,android,eclipse,sqlite,Android,Eclipse,Sqlite,我在安卓eclipse中创建了一个安卓应用程序项目,但登录命令有问题。 我已经注册,但无法登录,用户名和密码已使用sqlite浏览器在数据库中注册。但是当我输入用户名和密码时,我无法登录。我不知道错误在哪里。请帮忙 下面是我的mainactivity.java代码: package com.example.howtos; import com.example.howtos.DatabaseManager;
package com.example.howtos;
import com.example.howtos.DatabaseManager;
import com.example.howtos.MainActivity;
import com.example.howtos.R;
import com.example.howtos.reg;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseManager dataBase;
dataBase = DatabaseManager.instance();
Button b = (Button) findViewById (R.id.btn12);
b.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
try
{
final EditText username = (EditText) findViewById(R.id.editText1);
final EditText password = (EditText) findViewById(R.id.editText2);
final String COLUMN_PW = "password";
Cursor cursor = dataBase.select("SELECT password FROM users WHERE username = '" + username.getText().toString() + "';");
String s = cursor.getString(cursor.getColumnIndex(COLUMN_PW));
if(password.getText().toString() == s)
{
startActivity(new Intent(MainActivity.this,homepage.class));
Toast.makeText(getApplicationContext(), "Successfully Login", Toast.LENGTH_SHORT).show();
cursor.close();
}
}
catch(CursorIndexOutOfBoundsException e1)
{
Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show();
}
}
});
TextView r = (TextView) findViewById (R.id.textView7);
r.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
startActivity(new Intent(MainActivity.this,reg.class));
}
});
}
}
问题最有可能出现在这一行:
if(password.getText().toString() == s)
应成为:
if(password.getText().toString().equals(s))
在Java中,使用“==”总是比较对象引用,尽管两个字符串可能包含相同的值,但它们是不同的对象,因此比较总是失败的。因此,您应该对字符串使用“equals”方法,该方法将比较字符串值。这里可以找到一个全面的解释:
编辑:
第二个问题是您正在获得一个CursorIndexOutOfBoundsException(因为该异常的catch块是传递无效密码消息的位置)。打开光标后,您需要将其移动到第一行,然后才能尝试从中访问数据:
cursor.MoveToFirst();
此外,当密码/用户名错误时,您还需要显示错误消息。例如:
try
{
final EditText username = (EditText) findViewById(R.id.editText1);
final EditText password = (EditText) findViewById(R.id.editText2);
final String COLUMN_PW = "password";
Cursor cursor = dataBase.select("SELECT password FROM users WHERE username = '" + username.getText().toString() + "';");
cursor.MoveToFirst();
String s = cursor.getString(cursor.getColumnIndex(COLUMN_PW));
if(password.getText().toString().equals(s))
{
startActivity(new Intent(MainActivity.this,homepage.class));
Toast.makeText(getApplicationContext(), "Successfully Login", Toast.LENGTH_SHORT).show();
cursor.close();
}
else
{
Toast.makeText(getApplicationContext(), "Invalid Username or Password", Toast.LENGTH_SHORT).show();
}
}
catch(CursorIndexOutOfBoundsException e1)
{
Toast.makeText(getApplicationContext(), "Error accessing database", Toast.LENGTH_SHORT).show();
}
还记得在读取数据之前检查游标是否实际返回了任何数据(如果用户名不存在,则可能发生这种情况)。您可以通过检查cursor.getCount()的值来实现这一点。谢谢您的回复,但仍然不起作用。它只是保存在数据库中,但当我单击登录按钮时,它会显示“无效用户名或密码”。还有其他选项吗?非常感谢。我在上面的编辑中解释了第二个问题。它很有效!非常感谢@NigelK我会更仔细地研究你说了什么