在Android中强制关闭

时间:2012-08-06 01:30:36

标签: android android-activity crash forceclose

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

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

1 个答案:

答案 0 :(得分: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