Android:游标在SQLite数据库中出错

时间:2012-05-02 14:19:24

标签: android database sqlite cursor

我已经制作了一些完美的代码,但是在一些代码中进行了一些小的清理后,我得到了这个错误,并且我已经将它追溯到代码中的curser-action。它会记录Log.d("Start-up",name),但不记录Log.d("Start-up","Efter")。我真的无法弄清楚出了什么问题,我希望你能帮助我!

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

这是我的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)

编辑:全班

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


        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

好吧,从你的logcat我会看两个地方......

第87行的Indstillinger

第217行的DBHelpeR

发布这些代码部分,也许我们可以提供更多帮助。

修改

好的,我看到光标位已发布(刚刚醒来,对不起)。我真的没有看到该查询有什么问题(假设你已经定义ourDatabase,因为你没有在你发布的代码中显示),所以我会看看第87行的Indstillinger。这似乎是是错误日志中代码的第一个引用。

2ND EDIT

您正在使用一种方法,要求在打开数据库之前打开数据库。在调用doesNameExist之前执行entry.open()。

答案 1 :(得分:0)

没有特定错误,这只是猜测,但是:

  

在某些代码中进行了少量清理后,我收到了此错误

您是否更改了表格结构?如果是这样,您需要重建数据库,最简单的方法是增加DATABASE_VERSION。

<强>加成

空指针异常意味着一个变量,可能是ourDatabase,尚未定义。