我有一个游戏,它在SQLite数据库中存储了它的高分,但它只存储了我在我的应用程序中打开窗口的分数,显示了所述分数,如果我连续玩了2个游戏然后打开高分窗口,它只会显示后来的分数。所以我试着创建一个方法,我可以从主类调用,但我得到空引用错误。以下是我的Highscore课程,可以保存分数。我怎样才能做到这一点,即使没有打开"高分"也可以保存分数。窗口?
public class highscores extends Activity {
private ListView scorebox1;
private ListView scorebox2;
private ListView scorebox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.highscores);
DBHandler db = new DBHandler(this);
scorebox1 = (ListView) findViewById(R.id.scorebox1);
scorebox2 = (ListView) findViewById(R.id.scorebox2);
scorebox3 = (ListView) findViewById(R.id.scorebox3);
// Create database helper
DBHandler mDbHelper = new DBHandler(this);
// Gets the database in write mode
SQLiteDatabase db2 = mDbHelper.getWritableDatabase();
// Create a ContentValues object where column names are the keys,
// and pet attributes from the editor are the values.
if(difficutlyFrSQL != ""){
ContentValues values = new ContentValues();
values.put(DBHandler.KEY_TIME, difficutlyFrSQL);
values.put(DBHandler.KEY_DIFFICULTY, difficutlyFrSQL2);
long newRowId = db2.insert(DBHandler.TABLE_DETAIL, null, values);
// Show a toast message depending on whether or not the insertion was successful
if (newRowId == -1) {
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving test score", Toast.LENGTH_SHORT).show();
} else {
// Otherwise, the insertion was successful and we can display a toast with the row ID.
Toast.makeText(this, "Test score saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
}
difficutlyFrSQL = "";
}
Log.d("Reading: ", "Reading all scores..");
ArrayList<String>ar3=new ArrayList<>();
ar3=db.getAllScores(3);
Collections.sort(ar3);
Collections.reverse(ar3);
ArrayAdapter<String>ap3=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar3);
scorebox1.setAdapter(ap3);
ArrayList<String>ar2=new ArrayList<>();
ar2=db.getAllScores(2);
Collections.sort(ar2);
Collections.reverse(ar2);
ArrayAdapter<String>ap2=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar2);
scorebox2.setAdapter(ap2);
ArrayList<String>ar1=new ArrayList<>();
ar1=db.getAllScores(1);
Collections.sort(ar1);
Collections.reverse(ar1);
ArrayAdapter<String>ap1=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ar1);
scorebox3.setAdapter(ap1);
}
}