您好fokes我正在构建一个Android应用程序,我需要创建一个SQL数据库来节省时间和分数。
我还希望获得已保存的次数,以便我可以使用它来为用户提供播放次数。
基本上我需要sql创建10秒和0分。然后在每次比赛之后,我希望它有更多的时间让我们说10.01秒(或多或少无关紧要)并保存得分。
现在,当我运行游戏时,它可以工作,但是当它必须对sql数据库做任何事情时它会崩溃。
这是我的sql数据库java代码:
package com.orion.peky.thetapgame;
import android.content.ContentValues; import android.content.Context; import android.database.Cursor;进口 android.database.sqlite.SQLiteDatabase;进口 android.database.sqlite.SQLiteOpenHelper;
public class spremanje { public static final String KEY_ROWID =" _id&#34 ;; public static final String KEY_SCORE ="得分&#34 ;; public static final String KEY_TIME =" time&#34 ;;
private static final String IME_TABLICE = "bodovi"; private static final String TABELA_BAZE = "nmvz"; private static final int DATABASE_VERSION = 1; private DbHelper ourHelper; private final Context ourContext; private SQLiteDatabase ourDatabase; private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, IME_TABLICE, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABELA_BAZE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TIME + " INTEGER , " + KEY_SCORE + "INTEGER )" ); ContentValues cv = new ContentValues(); int time=10000; cv.put(KEY_TIME, time); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABELA_BAZE); onCreate(db); } } public spremanje(Context c) { ourContext = c; } public spremanje open() { ourHelper = new DbHelper(ourContext); ourDatabase = ourHelper.getWritableDatabase(); return this; } public void close() { ourHelper.close(); } public long createEntry(int time, int score) { ContentValues cv = new ContentValues(); cv.put(KEY_TIME, time); cv.put(KEY_SCORE, score); return ourDatabase.insert(IME_TABLICE, null, cv); } public int getRed() { String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE}; Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null); int red = 0; int iRow = c.getColumnIndex(KEY_ROWID); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { red = c.getInt(iRow); } return red; } public int getTime() { String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE}; Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null); int time = 10000; int iTime = c.getColumnIndex(KEY_TIME); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { time = c.getInt(iTime); } return time; } public int getScore() { String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE}; Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null); int score = 0; int iScore = c.getColumnIndex(KEY_SCORE); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { score = c.getInt(iScore); } return score; } }
这是我的游戏活动:
package com.orion.peky.thetapgame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.ads.*;
public class Game extends Activity {
TextView tekst, vrijeme;
int brojac=0, provjera=0;
CountDownTimer Count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
spremanje read = new spremanje(this);
read.open();
int brojacvremena=read.getTime();
read.close();
tekst=(TextView)findViewById(R.id.tekst);
vrijeme=(TextView)findViewById(R.id.vrijeme);
final Intent i =new Intent(Game.this,Score.class);
vrijeme.setText("Time: "+brojacvremena/1000 + "." + brojacvremena%1000);
/*AdView adView=(AdView)this.findViewById(R.id.adView123);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);*/
Count = new CountDownTimer(brojacvremena, 1) {
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
vrijeme.setText(seconds + "." + millisUntilFinished % 1000);
}
public void onFinish(){
i.putExtra("prijenos",brojac);
startActivity(i);
finish();
}};
}
public void broji(View view){
if(provjera==0){
Count.start();
brojac++;
provjera++;
tekst.setText("Taps " + brojac);
}else {
brojac++;
tekst.setText("Taps " + brojac);
}
}
}
以下是我的游戏活动后的屏幕:
package com.orion.peky.thetapgame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Score extends Activity {
int primljeno=1, vrijeme=10000;
Bundle dodatak;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
TextView prikazati=(TextView)findViewById(R.id.prikazbodova);
Intent inte=getIntent();
dodatak=inte.getExtras();
primljeno=dodatak.getInt("prijenos");
prikazati.setText("You tapped "+primljeno+" times");
spremanje unos = new spremanje(this);
unos.open();
vrijeme= unos.getTime();
vrijeme=vrijeme+125;
unos.createEntry(vrijeme,primljeno);
unos.close();
}
}
这是我的logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orion.peky.thetapgame/com.orion.peky.thetapgame.Game}: android.database.sqlite.SQLiteException: no such table: bodovi: , while compiling: SELECT _id, time, score FROM bodovi
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: bodovi: , while compiling: SELECT _id, time, score FROM bodovi
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
at com.orion.peky.thetapgame.spremanje.getTime(spremanje.java:87)
at com.orion.peky.thetapgame.Game.onCreate(Game.java:22)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
我已经研究过sql数据库还有一件事,但似乎我找不到问题,所以请不要将其标记为副本,可能会有1个以上的错误。感谢至少阅读。
答案 0 :(得分:0)
解决了这个结果。
@覆盖 public void onCreate(SQLiteDatabase db){ db.execSQL(&#34; CREATE TABLE&#34; + IME_TABLICE +&#34;(&#34; +
KEY_ROWID +&#34; INTEGER PRIMARY KEY AUTOINCREMENT,&#34; + KEY_TIME +&#34; INTEGER,&#34; + KEY_SCORE +&#34; INTEGER)&#34;