每当我尝试打开sqlite数据库时,我的应用程序崩溃

时间:2014-10-15 08:54:17

标签: android sqlite

你好,我正在为大学生和学生开发一个应用程序。我正在尝试添加sqlite数据库,但是当我尝试将数据插入数据库时​​,如应用程序中的学生注册,当我按下按钮注册应用程序崩溃实际上我是新的Android世界N我看到一些教程知道该怎么做>>> N我不知道这是什么错误 希望有人能帮助我

继承我的代码

这是openhelper

public class DataBaseopretions extends SQLiteOpenHelper {
public static final int database_version=1;
public String CREATE_QUERY="CREATE_TABLE"+Tableinfo.TABLE_NAME+"("+ Tableinfo.ST_ID +"  TEXT,"
        + Tableinfo.ST_pass + "  TEXT);";

public DataBaseopretions(Context context) {
    super(context, Tableinfo.TABLE_NAME , null, database_version);
    // TODO Auto-generated constructor stub
    Log.d("Database opreations", " database created");
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_QUERY);
    Log.d("Database opreations", " table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}



public void putinformation(DataBaseopretions dop ,String st_id ,String pass){

    SQLiteDatabase SQ=dop.getWritableDatabase();
    ContentValues CV=new ContentValues();
    CV.put(Tableinfo.ST_ID, st_id);
    CV.put(Tableinfo.ST_pass, pass);
    long k= SQ.insert(Tableinfo.TABLE_NAME, null, CV);
    Log.d("Database opreation", "one row insearted");
}

}

这是我创建数据库的地方

public class Tabledata {

public Tabledata(){

}

public static abstract class Tableinfo implements BaseColumns {

    public static final String ST_ID="studentId";
    public static final String ST_pass="pass";
    public static final String DATABASE_NAME="colleg";
    public static final String TABLE_NAME="student";
}

}

这是注册

 public class Rigstretion extends Activity  {
 EditText st_id , st_pass, Pass_again;
 String student_id ,pass_ag ,pass;
 Button regs, cancel_reg;
  Context cxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registor);
        st_id=(EditText)findViewById(R.id.st_id);
        st_pass=(EditText)findViewById(R.id.st_pass);
        Pass_again=(EditText)findViewById(R.id.pass_again);

        regs=(Button)findViewById (R.id.regs) ;
        cancel_reg=(Button) findViewById(R.id.cancel_reg);
        regs.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                student_id=st_id.getText().toString();
                pass=st_pass.getText().toString();
                pass_ag=Pass_again.getText().toString();

                if(!(pass.equals(pass_ag)))
                {
                    Toast.makeText(getBaseContext(), "Password not match", Toast.LENGTH_LONG).show();
                    st_id.setText("");
                    st_pass.setText("");
                    Pass_again.setText("");

                }
                else
                {
                    DataBaseopretions db =new DataBaseopretions(cxt);
            db.putinformation(db, student_id, pass);
            Toast.makeText(getBaseContext(), "Registrtion success", Toast.LENGTH_LONG).show();
            finish();
                }

            }
        });


    }

&安培;这个logcat

10-15 08:49:05.477: W/SocketClient(110): write error (Broken pipe)
10-15 08:49:06.213: D/OpenGLRenderer(1057): TextureCache::get: create texture(0xb804b280): name, size, mSize = 185, 3328, 1618848
10-15 08:49:06.281: W/audio_hw_primary(116): out_write() limiting sleep time 46802 to 39909
10-15 08:49:06.285: D/Database opreations(1057):  database created
10-15 08:49:06.285: D/AndroidRuntime(1057): Shutting down VM
10-15 08:49:06.285: W/dalvikvm(1057): threadid=1: thread exiting with uncaught exception (group=0xa6265288)
10-15 08:49:06.293: E/AndroidRuntime(1057): FATAL EXCEPTION: main
10-15 08:49:06.293: E/AndroidRuntime(1057): java.lang.NullPointerException
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at za.example.drawer.DataBaseopretions.putinformation(DataBaseopretions.java:40)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at za.example.drawer.Rigstretion$1.onClick(Rigstretion.java:48)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.view.View.performClick(View.java:4084)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.view.View$PerformClick.run(View.java:16966)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.os.Handler.handleCallback(Handler.java:615)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.os.Looper.loop(Looper.java:137)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-15 08:49:06.293: E/AndroidRuntime(1057):     at dalvik.system.NativeStart.main(Native Method)
10-15 08:49:06.293: W/ActivityManager(394):   Force finishing activity za.example.drawer/.Rigstretion
10-15 08:49:06.341: E/SocketStream(437): readFully was waiting for 275772 bytes, got 49152 
10-15 08:49:06.341: E/SocketStream(437): readFully was waiting for 226620 bytes, got 49152 
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 177468 bytes, got 49152 
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 128316 bytes, got 49152 
10-15 08:49:06.345: E/SocketStream(437): readFully was waiting for 79164 bytes, got 492 
10-15 08:49:06.349: E/SocketStream(437): readFully was waiting for 78672 bytes, got 49152 
10-15 08:49:06.353: E/SocketStream(437): readFully was waiting for 29520 bytes, got 16384 
10-15 08:49:06.357: W/audio_hw_primary(116): out_write() limiting sleep time 59274 to 39909
10-15 08:49:06.365: W/audio_hw_primary(116): out_write() limiting sleep time 49591 to 39909
10-15 08:49:06.377: D/dalvikvm(394): GC_FOR_ALLOC freed 494K, 9% free 9185K/10055K, paused 14ms, total 17ms
10-15 08:49:06.417: W/audio_hw_primary(116): out_write() limiting sleep time 69546 to 39909
10-15 08:49:06.453: W/audio_hw_primary(116): out_write() limiting sleep time 79501 to 39909
10-15 08:49:06.493: D/(394): HostConnection::get() New Host Connection established 0xb7f9c4b8, tid 408
10-15 08:49:06.493: W/audio_hw_primary(116): out_write() limiting sleep time 59455 to 39909
10-15 08:49:06.881: W/ActivityManager(394): Activity pause timeout for ActivityRecord{5358f428 za.example.drawer/.Rigstretion}
10-15 08:49:16.385: W/ActivityManager(394): Launch timeout has expired, giving up wake lock!
10-15 08:49:16.893: W/ActivityManager(394): Activity idle timeout for ActivityRecord{535c3ef0 za.example.drawer/.MainActivity}
10-15 08:49:31.553: W/ThrottleService(394): unable to find stats for iface rmnet0
10-15 08:49:31.561: W/SocketClient(110): write error (Broken pipe)
10-15 08:49:32.805: D/OpenGLRenderer(510): TextureCache::get: create texture(0xb8101c90): name, size, mSize = 51, 1440, 162152
10-15 08:50:05.497: W/SocketClient(110): write error (Broken pipe)

我搜索了解决方案N没有任何工作......提前谢谢^^

3 个答案:

答案 0 :(得分:1)

谢谢你们,我发现了什么错误..它在create table

oncreate ()附近

我所做的就是在CREATE_QUERY删除public class DataBaseopretions extends SQLiteOpenHelper并在onecreate()方法中编写了创建表语句 像这样,它的工作原理

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + Tableinfo.DATABASE_TABLE + " (" + Tableinfo.ST_ID + " INTEGER PRIMARY KEY , "  +
            Tableinfo.ST_PASS +" TEXT NOT NULL, " +  Tableinfo.PASS_AG  + " TEXT NOT NULL );  "
                    );

答案 1 :(得分:0)

添加

ctx=this;

在onCreate()方法中。

答案 2 :(得分:0)

cxt var未初始化,因此将null作为参数传递给新DataBaseopretions。无论如何,cxt是无用的,就这样做:

DataBaseopretions db =new DataBaseopretions(Rigstretion.this);