0

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

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

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();
        }
    }
}
4

2 回答 2

0

这就是您收到错误的原因

// 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);
于 2012-08-11T05:58:34.490 回答
0

SelesMeter2Activity您将有一个NullPointerException在线:

if (c.getCount() != 0) {

因为您没有Cursor在该行之前初始化。将查询移到上述行之前:

c = sql.rawQuery("select * from Employee", null);
if (c.getCount() != 0) {
     // ...

您应该发布从 logcat 获得的异常。

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

于 2012-08-11T05:58:54.767 回答