简体   繁体   English

Android:SQLite数据库中的光标出现错误

[英]Android: Error with cursor in a SQLite database

I've made some code which worked perfectly, but after a minor clean-up in some code i get this error, and i've traced it down to the curser-action in the code. 我编写了一些效果很好的代码,但是在对某些代码进行较小的清理之后,我得到了这个错误,并且我将其追溯到代码中的curser-action。 It logs the Log.d("Start-up",name) but not the Log.d("Start-up","Efter") . 它记录Log.d("Start-up",name)而不记录Log.d("Start-up","Efter") I really cant figure out what went wrong, i hope you can help me! 我真的不知道出了什么问题,希望您能帮助我!

db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + " (" + 
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NAME + " TEXT NOT NULL, " +
            PLAYER_1 + " TEXT NOT NULL, " +
            PLAYER_2 + " TEXT NOT NULL, " +
            PLAYER_3 + " TEXT NOT NULL, " +
            PLAYER_4 + " TEXT NOT NULL, " +
            PLAYER_5 + " TEXT NOT NULL, " +
            ANTAL + " TEXT NOT NULL, " +
            OVERSIDDER + " TEXT NOT NULL, " +
            G + " TEXT NOT NULL, " +
            LOFT + " TEXT NOT NULL);"
            );




    public boolean doesNameExist(String name){
    String[] columns = new String[]{NAME};
    Log.d("Start-up",name);
    Cursor c = ourDatabase.query(DATABASE_TABLE2, columns, NAME + " = '"+ name + "'", null, null, null, null);
    Log.d("Start-up","Efter");
    return c.moveToFirst();
}

Here is my logcat: 这是我的日志:

05-02 16:20:58.168: W/dalvikvm(22533): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
05-02 16:20:58.178: E/AndroidRuntime(22533): FATAL EXCEPTION: main
05-02 16:20:58.178: E/AndroidRuntime(22533): java.lang.NullPointerException
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.jesper.whist.DBHelpeR.doesNameExist(DBHelpeR.java:217)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.jesper.whist.Indstillinger$2.onClick(Indstillinger.java:87)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.view.View.performClick(View.java:2408)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.view.View$PerformClick.run(View.java:8817)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Handler.handleCallback(Handler.java:587)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.os.Looper.loop(Looper.java:144)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at android.app.ActivityThread.main(ActivityThread.java:4937)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at java.lang.reflect.Method.invoke(Method.java:521)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-02 16:20:58.178: E/AndroidRuntime(22533):    at dalvik.system.NativeStart.main(Native Method)

EDIT: Entire class 编辑:整个类

package com.jesper.whist;

public class Indstillinger extends Activity{

//Variables
public static final String MY_SETTINGS = "MySettings";
EditText name,G,loft,player1,player2,player3,player4,player5;
String antal;
TextView playertext5, overskrift;
Typeface tf;
CheckBox femSpiller;
SharedPreferences settings;
Toast display;
int id;
Button ok_inst;
DBHelpeR entry;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Remove app-name
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Set heading font
    setContentView(R.layout.indstillinger);
    tf = Typeface.createFromAsset(getAssets(), "FargoFaroNF.ttf");
    overskrift = (TextView) findViewById(R.id.indstillinger);
    overskrift.setTypeface(tf);

    // Setting up Shared prefenceres and database
    settings = getSharedPreferences(MY_SETTINGS, 0);
    entry = new DBHelpeR(Indstillinger.this);

    // More variables
    G = (EditText) findViewById(R.id.etG);
    G.setText("15");
    loft = (EditText) findViewById(R.id.etLoft);
    loft.setText("10000");
    name = (EditText) findViewById(R.id.etName);
    player1 = (EditText) findViewById(R.id.etPlayer1);
    player2 = (EditText) findViewById(R.id.etPlayer2);
    player3 = (EditText) findViewById(R.id.etPlayer3);
    player4 = (EditText) findViewById(R.id.etPlayer4);
    player5 = (EditText) findViewById(R.id.etPlayer5);
    playertext5 = (TextView) findViewById(R.id.tvPlayer5);
    femSpiller = (CheckBox) findViewById(R.id.cb_fem);
    ok_inst = (Button) findViewById(R.id.bInst);

    // Toast
    display = Toast.makeText(this, "Navnet er optaget! Vælg et nyt!", Toast.LENGTH_SHORT);

    // Get id from Shared Preferences
    id = settings.getInt("game_id", 0);

    // Hvis 5-spillere-checkboxen checkes, vis indstillingsmuligheder
    femSpiller.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
            playertext5.setVisibility(View.VISIBLE);    // Vis TextView
            player5.setVisibility(View.VISIBLE);    // Vis EditText
        }
        });

    // Ved klik på ok-knappen
    ok_inst.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            // Check if name exist
            if(entry.doesNameExist(name.getText().toString())==true){
                display.show();     // Vis toast
            }
            // Hvis navnet ikke eksistere
            else{
                if(femSpiller.isChecked()){antal = "5";}    // Hvis der er 5 spillere
                else{antal = "4";}      // Hvis der er 4 spillere


                entry.open();

                // Lav entry i Spil_table: spil navn, spillernavne, antal, oversidder=5 så sidste man sidder over i første runde, grundtakst, loft
                entry.createEntrySpil(name.getText().toString(), player1.getText().toString(), player2.getText().toString(), player3.getText().toString(), player4.getText().toString(), player5.getText().toString(), antal,"5", G.getText().toString(), loft.getText().toString());
                entry.createEntryPenge(id,"0","0","0","0","0"); // Lav en nul-entry så det virker
                entry.close();

                // Start WHIST! og afslut denne proces
                startActivity(new Intent("com.jesper.whist.WHIST"));
                finish();
                }


        }
    });
}

} }

Well, from your logcat I would look in two places... 好吧,从您的logcat来看,我将在两个地方查看...

Indstillinger around line 87 第87行附近的Indstillinger

and

DBHelpeR around line 217 围绕217行的DBHelpeR

Post those sections of code and maybe we can be a bit more helpful. 发布这些代码部分,也许我们会有所帮助。

EDIT 编辑

Ok, I see that the cursor bit is posted (just waking up, sorry). 好的,我看到光标位已发布(刚刚醒来,对不起)。 I really don't see anything wrong with that query (assuming you have defined ourDatabase since you don't show that in the code you posted), so I'd look at the Indstillinger at line 87. That seems to be the first reference to your code in the error log. 我确实没有发现该查询有任何问题(假设您已经定义了ourDatabase因为您没有在发布的代码中显示该信息),所以我将看一下第87行的Indstillinger。这似乎是第一个参考错误日志中的代码。

2ND EDIT 第二编辑

You are using a method that requires the DB to be open before you open it. 您正在使用一种要求在打开数据库之前将其打开的方法。 Do entry.open() before you call doesNameExist. 在调用dosNameExist之前,请执行entry.open()。

Without a specific error this is just a guess, but: 没有特定的错误,这只是一个猜测,但是:

after a minor clean-up in some code i get this error 经过一些代码的小幅清理后,我得到此错误

Did you change the table structure? 您更改了表格结构吗? If so you need to rebuild the database, the easiest way to do this is to increment the DATABASE_VERSION. 如果是这样,则需要重建数据库,最简单的方法是增加DATABASE_VERSION。

Addition 加成

A null pointer exception means a variable, probably ourDatabase , hasn't been defined. 空指针异常表示尚未定义变量,可能是ourDatabase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM