0

我是 Android 编程新手,对 java 很生疏!我一直在开发一个小程序,要求用户向 EditText 输入一些内容,一旦时间停止或用户单击“完成”按钮,它就会在数据库中搜索用户设置的值。也就是说,我在那部分遇到了错误。:(

public class UserScreen extends Activity implements View.OnClickListener {

    public TextView tvCounter;
    public EditText input;
    public Button finish;

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamescreen);

        tvCounter = (TextView) findViewById(R.id.tvCounter);
        finish = (Button) findViewById(R.id.bFinish);
        finish.setOnClickListener(this);

        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {
                tvCounter.setText("seconds remaining: "
                        + Long.toString(millisUntilFinished / 1000));
            }

            public void onFinish() {
                tvCounter.setText("done!");
                searchDB();
            }
        }.start();
    }

    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;

    public void searchDB() {
        Object tableList[] = { "table1", "table2", "table3", "table4",
                "table5", "table6" };
        Object columnList[] = { "column1", "column2", "column3", "column4", "column5", "column6" };
        int pList[] = { R.id.et1, R.id.et2, R.id.et3,
                R.id.et4, R.id.et5, R.id.et6 };

        database = dbHelper.getReadableDatabase();
        Cursor cursor = null;

        for (int i = 0; i < tableList.length; i++) {

            input = (EditText) findViewById(pList[i]);
            String data = input.getText().toString();

            cursor = database.rawQuery("SELECT * FROM " + tableList[i]
                    + " WHERE " + pList[i] + " = " + columnList[i], null);

            String dbStuff = cursor.toString();
                        if (dbStuff == data) { 
                            tvCounter.setText("YEAH");
                        } 
                        else { 
                        tvCounter.setText("DAMN"); 
                        }
        }
        cursor.close();
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        searchDB();
    }
}

错误是:

07-28 17:43:59.385: E/AndroidRuntime(920): FATAL EXCEPTION: main
07-28 17:43:59.385: E/AndroidRuntime(920): java.lang.NullPointerException
07-28 17:43:59.385: E/AndroidRuntime(920): at com.example.androidgame.GameScreen.searchDB(GameScreen.java:56)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.example.androidgame.GameScreen.onClick(GameScreen.java:84)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.view.View.performClick(View.java:3511)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.view.View$PerformClick.run(View.java:14105)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Handler.handleCallback(Handler.java:605)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.os.Looper.loop(Looper.java:137)
07-28 17:43:59.385: E/AndroidRuntime(920): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-28 17:43:59.385: E/AndroidRuntime(920): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 17:43:59.385: E/AndroidRuntime(920): at java.lang.reflect.Method.invoke(Method.java:511)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-28 17:43:59.385: E/AndroidRuntime(920): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-28 17:43:59.385: E/AndroidRuntime(920): at dalvik.system.NativeStart.main(Native Method)

创建或访问 DB 在另一个类中。

公共类 DBAccess 扩展 Activity {

private DatabaseHelper databaseHelper;
private SQLiteDatabase db;


public void onCreate(Context context){
    databaseHelper = new DatabaseHelper(context);
    searchDB();
}

public void openDB(){
    db = databaseHelper.getWritableDatabase();
}
public void closeDB(){
    db.close();
}
public void searchDB(){
    Intent Scoring = getIntent();
    String rawdata[] = Scoring.getStringArrayExtra("data");
    openDB();
    Object tableList[] = { "table1", "table2", "table3", "table4", "table5", "table6" };
    Object columnList[] = { "KEY_COLUMN1", "KEY_COLUMN2", "KEY_COLUMN3", "KEY_COLUMN4", "KEY_COLUMN5",
    "KEY_COLUMN6" };

    int downScore = 0;
    int upScore = 0;
    String dbStuff;

    Cursor cursor = null;

    for (int i = 0; i < tableList.length; i++) {
        if(rawdata[i] == "niente")
            downScore = downScore + 5;
        else {
            cursor = db.rawQuery("SELECT * FROM " + tableList[i] + " WHERE " + rawdata[i] + " = " + columnList[i] , null);
        dbStuff = cursor.toString();
        if (i == 0)
             cursor.moveToFirst();
        else
            cursor.moveToNext();
        if(dbStuff == rawdata[i])
            upScore = upScore + 10;
        else
            downScore = downScore + 5;
        }
    }
    closeDB();
    cursor.close();
    Intent i = new Intent(this, ScoreScreen.class);
    i.putExtra("uScore", upScore);
    i.putExtra("dScore", downScore);
    }

}

4

2 回答 2

1

设计方面,将数据库构建和维护方面与数据库访问层本身分开。

为此,有 2 节课。1)将所有的构建和维护放在DBHelper.java中,2)DBAccess.java有访问数据库的方法。

下面是这个的代码片段......请注意,应用程序(活动)本身应该一无所知,并且对数据库本身的构造没有可见性(参见 DBHelper 类本身的可见性),而是对简单访问具有完全可见性方法,例如打开、关闭、CRUD 方法...


:

class DBHelper extends SQLiteOpenHelper {

    :
    :

    private SQLiteDatabase db;
    private static final int DATABASE_VERSION = 1;
    private static final String DB_NAME = "dbname.db";
    :
    :

}

public class DBAccess {

    :
    :

    private DBHelper dbHelper;
    private SQLiteDatabase db;

    :
    :

    public DBAccess(Context context) {
        dbHelper = new DBHelper(context);
    }

    /**
     * Open the database
     */
    public void openDB() {
        db = dbHelper.getWritableDatabase();
    }

    /**
     * Close the database
     */
    public void closeDB() {
        db.close();
    }

    :
    :
}

活动使用数据库,您可以有一个名为 setupDB() 的方法,该方法在活动创建期间调用:

private void setupDB() {
    dbAccess = new DBAccess(this);
    dbAccess.openDB();
}

:

private void searchDB() {
    :

    mStatusList = dbAccess.selectAllStatus();

    :
}

:
:
于 2012-07-28T21:26:04.620 回答
0

您需要初始化您的数据库,它的助手在onCreate()

  database = new SQLiteDatabase();
  dbHelper = new DatabaseHelper();
于 2012-07-28T18:04:52.267 回答