Android SQL查询和强制关闭挑战

Android SQL查询和强制关闭挑战,android,nullpointerexception,android-sqlite,Android,Nullpointerexception,Android Sqlite,在这里,我试图从数据库中获取用户名和密码,如果用户已经有一个帐户,或者通过输入他的数据注册他 这是第一个通过单击第一个按钮登录数据库而强制关闭的活动 public class SelesMeter2Activity extends Activity implements OnClickListener { EditText ed1; EditText ed2; Button b1; Button b2; SQLiteDatabase sql; Cu

在这里,我试图从数据库中获取用户名和密码,如果用户已经有一个帐户,或者通过输入他的数据注册他

这是第一个通过单击第一个按钮登录数据库而强制关闭的活动

public class SelesMeter2Activity extends Activity implements OnClickListener {
    EditText ed1;
    EditText ed2;
    Button b1;
    Button b2;
    SQLiteDatabase sql;
    Cursor c;
    Intent in;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed1 = (EditText) findViewById(R.id.ed1);
        ed2 = (EditText) findViewById(R.id.ed2);
        b1 = (Button) findViewById(R.id.bt1);
        b2 = (Button) findViewById(R.id.bt2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        sql = openOrCreateDatabase("db", 0, null);
        sql.execSQL("CREATE TABLE if not exists "
                + "Employee2 (password integer NOT NULL PRIMARY KEY,name text NOT NULL)");
    }

    @Override
    public void onClick(View arg0) {
        // log in
        if (arg0.getId() == R.id.bt1) {
            int p = 0;
            String name = ed1.getText().toString();
            String sp = ed2.getText().toString();
            try {
                // Attempt to parse the number as an integer
                p = Integer.parseInt(sp);
            } catch (NumberFormatException nfe) {
                // parseInt failed, so tell the user it's not a number
                Toast.makeText(this,
                        "Sorry, " + sp + " is not a number. Please try again.",
                        Toast.LENGTH_LONG).show();
            }
            if (c.getCount() != 0) {
                c = sql.rawQuery("select * from Employee", null);
                while (c.moveToNext()) {
                    if (name.equals("c.getString(1)") && p == c.getInt(0)) {
                        in = new Intent(this, secondview.class);
                        startActivity(in);
                        break;
                    }
                }
            }

            else {
                Toast.makeText(this,
                        "please sign up first or enter " + "correct data", 2000)
                        .show();
            }

        } else if (arg0.getId() == R.id.bt2) {
            // sign up
            Intent in2 = new Intent(this, signup.class);

            startActivity(in2);

        }
    }
}
输入未按预期工作的新用户的第二个类, 祝酒词不起作用:

public class signup extends Activity implements OnClickListener {
    EditText e1;
    EditText e2;
    SQLiteDatabase sql;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singupx);
        Intent in = getIntent();
        e1 = (EditText) findViewById(R.id.ed1s);
        e2 = (EditText) findViewById(R.id.ed2s);
        b = (Button) findViewById(R.id.bt1s);
        b.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String n = e1.getText().toString();
        String sp = e2.getText().toString();
        try {
            // Attempt to parse the number as an integer
            int p = Integer.parseInt(sp);
            // This insertion will *only* execute if the parseInt was successful
            // sql.execSQL("insert into Employee2(password,name)values('"+n+"',"+p+")");
            ContentValues values = new ContentValues();
            values.put("password", p);
            values.put("name", n);
            sql.insert("Employee2", null, values);
            Toast.makeText(this, "Data inserted", Toast.LENGTH_LONG).show();
            Intent in2 = new Intent(this, secondview.class);
            startActivity(in2);
        } catch (NumberFormatException nfe) {
            // parseInt failed, so tell the user it's not a number
            Toast.makeText(this,
                    "Sorry, " + sp + " is not a number. Please try again.",
                    Toast.LENGTH_LONG).show();
        }
    }
}

这就是为什么会出现错误

// you are calling the `c.getCount();` before you are assigning
// It will throw null pointer exception
if (c.getCount() != 0) {
    c = sql.rawQuery("select * from Employee", null);
    while (c.moveToNext()) {
        if (name.equals(c.getString(1)) && p == c.getInt(0)) {
            in = new Intent(this, secondview.class);
            startActivity(in);
            break;
        }
    }
}
像这样改变逻辑

c = sql.rawQuery("select * from Employee", null);
c.moveToFirst();
if(!c.isAfterLast()) {
    do {
        if (name.equals(c.getString(1)) && p == c.getInt(0)) {
            in = new Intent(this, secondview.class);
            startActivity(in);
            break;
        }
    } while (c.moveToNext());
}
name.equals(“c.getString(1)”)
应该是
name.equals(c.getString(1))

编辑

插入法示例

ContentValues values = new ContentValues();
values.put("password", n);
values.put("name", p);
database.insert("Employee2", null, values);

SelesMeter2Activity
中,您将在以下行出现
NullPointerException

if (c.getCount() != 0) {
因为您没有在该行之前初始化
光标
。将查询移动到上一行之前:

c = sql.rawQuery("select * from Employee", null);
if (c.getCount() != 0) {
     // ...
您应该发布从logcat获得的异常


另外,关于您的
注册
活动,请不要实例化从中访问字段的第一个活动。在
第二个
活动中再次打开数据库并插入值。

您可以发布日志跟踪吗?您需要具有相同的
sql=openOrCreateDatabase(“db”,0,null)语句。是的,您是对的:)错误已消失,但注册活动未将数据输入数据库!祝酒辞没有什么意义work@SarahAabed你指的是哪个祝酒词?注册活动是否运行?是的,它已运行,但sql查询后的toast不起作用,因此我无法输入数据@LuKsprong@SarahAabed您是否修改了注册以不实例化
SelesMeter2Activity
活动,而是直接打开数据库?您是否已检查logcat以了解有关插入的任何警告。另外,请考虑使用一个代替<代码> Excel SQL >代码来实际查看插入是否正确。@ SalaHabeBe您解决了这个问题吗?您没有看到任何
祝酒
,因为您没有在
注册
活动中为
按钮
设置
OnClickListener
(在布局中找到按钮(就像您为
编辑文本
所做的那样)并分配侦听器
按钮。setOnClickListener(此);
)。另外,我建议您使用
SQLiteOpenHelper
来管理数据库。是的,没错:)注册活动如何?它不接受数据并将其输入数据中base@SarahAabed同一个包中的这两个类?@SarahAabed ok,正如Luksprog所说,更改实现。我确信,
sql
仍然没有为不同的上下文保留数据库的引用,因为您正在实例化新对象,而不是启动它
onCreate()
这是每个活动的入口点,您在这里进行数据库初始化。我已经更改了逻辑,我将尝试使用插入方法,但您的意思是我必须将其放在onCreate中吗?再次感谢:)