我是 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);
}
}