2

运行我的应用程序时出现空指针异常,我必须强制关闭。我想我无法以正确的形式管理数据库。

package sarah.android;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SelesMeter2Activity extends Activity implements OnClickListener {
    EditText ed1;
    EditText ed2;
    Button b1;
    Button b2;
    Intent in;
    signup obj=new signup();

    /** 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);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //log in
        if (arg0.getId() == R.id.bt1)
        {
            String name=ed1.getText().toString();
            int pass = Integer.parseInt(ed2.getText().toString());
            if (obj.c.getString(0) == name&&obj.c.getInt(1) == pass)
            {
                in = new Intent(this,secondview.class);
                startActivity(in);
            }
            else
            {
                Toast.makeText(this, "please sing up first", 1000).show();
            }
        }
        else
            if (arg0.getId()==R.id.bt2)
            {
                in=new Intent(this,signup.class);
                startActivity(in);
            }
    }
}

用于注册新用户并将他们的信息放入我的数据库的第二个活动:

package sarah.android;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Toast;

public class signup extends Activity {
    SQLiteDatabase sql;
    Cursor c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singupx);
        Intent in = getIntent();
        Bundle b = in.getExtras();
        String n = b.getString("x");
        int p = Integer.valueOf(b.getString("y"));

        sql = openOrCreateDatabase("db",0, null);
        sql.execSQL("CREATE TABLE if not exists employee " +
                    " (name text NOT NULL UNIQUE" +
                    ",password integer NOT NULL UNIQUE)");
        sql.execSQL("insert into employee(name) values(" +
                    "'"+n+"') ");
        Toast.makeText(this,"Data inserted", 1000).show();
    }
}
4

1 回答 1

3

这……很简单。在您的第一个活动中,您没有投入"x""y"投入意图。在第二个活动中,你试图得到它们。

...
Bundle b=in.getExtras(); // perhaps this is null
String n=b.getString("x"); // or this one
int p=Integer.valueOf(b.getString("y")); // or this one
于 2012-08-06T02:44:00.797 回答