我是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是另一个类。
public class 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);
}
}
答案 0 :(得分:1)
从DB访问层本身设计方面,单独的DB构造和维护方面。
为此,有2个班级。 1)在DBHelper.java中放入所有的构造和维护,2)DBAccess.java有访问DB的方法。
以下是此代码片段...请注意,应用程序(活动)本身应该什么都不知道,并且对DB本身的构造没有可见性(请参阅DBHelper类本身的可见性),而只是具有完全可见性简单的访问方法,如open,close,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();
}
:
:
}
使用DB的活动,你可以有一个名为setupDB()的方法,在创建活动期间调用的方法:
private void setupDB() {
dbAccess = new DBAccess(this);
dbAccess.openDB();
}
:
private void searchDB() {
:
mStatusList = dbAccess.selectAllStatus();
:
}
:
:
答案 1 :(得分:0)
您需要在onCreate()
:
database = new SQLiteDatabase();
dbHelper = new DatabaseHelper();