gpt4 book ai didi

java - Android:虚拟显示器和真实电话问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:47 24 4
gpt4 key购买 nike

昨晚到现在为止,我遇到了一个严重的问题,这个项目(带有外部数据库)在虚拟监视器中运行得很好

API 22, Android 1.5(Google APIs)

但是如果我用手机跑进来,

(Android 5.1, API 22)

它返回一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hamira.wordmatchinggame, PID: 12466
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hamira.wordmatchinggame/com.example.hamira.wordmatchinggame.LeaderBoard}: android.database.sqlite.SQLiteException: no such table: player (code 1): , while compiling: SELECT _id, pname, escr, dscr, hscr, tscr FROM player ORDER BY tscr DESC
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2525)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2617)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:218)
at android.app.ActivityThread.main(ActivityThread.java:5657)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:990)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785)
Caused by: android.database.sqlite.SQLiteException: no such table: player (code 1): , while compiling: SELECT _id, pname, escr, dscr, hscr, tscr FROM player ORDER BY tscr DESC
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1193)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1064)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1270)
at com.example.hamira.wordmatchinggame.DataConn.displayLeaderBoard(DataConn.java:187)
at com.example.hamira.wordmatchinggame.LeaderBoard.list(LeaderBoard.java:79)
at com.example.hamira.wordmatchinggame.LeaderBoard.onCreate(LeaderBoard.java:35)
at android.app.Activity.performCreate(Activity.java:6135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2472)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2617) 
at android.app.ActivityThread.access$800(ActivityThread.java:182) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:218) 
at android.app.ActivityThread.main(ActivityThread.java:5657) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:990) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785) 
I/Process: Sending signal. PID: 12466 SIG: 9
Application terminated.

通过我的代码:

MainActivity.java

public class LeaderBoard extends Activity {
DataConn myDb;
Intent back;
AlertDialog.Builder bld;
String[] fromFieldNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
back = new Intent(LeaderBoard.this, MainActivity.class);
myDb = new DataConn(this);
list();
});

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void list() {
Cursor csr = myDb.displayLeaderBoard(); //<-----------error

fromFieldNames = new String[]{"pname", "escr", "dscr", "hscr", "tscr"};
int[] toView = new int[]{R.id.layoutpname, R.id.layouteasyscore, R.id.layoutdifficultscore, R.id.layouthardscore, R.id.layouttotalscore};
SimpleCursorAdapter myAdapter;
myAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.lblist, csr, fromFieldNames, toView, 0);
ListView PlayerList = (ListView) findViewById(R.id.listplayer);
PlayerList.setAdapter(myAdapter);
}

DataConn.java

public class DataConn extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_egame.db";
private static final String DB_PATH = "data/data/com.example.hamira.wordmatchinggame/databases/";
private static final String TABLE_NAME = "easy";
private SQLiteDatabase db;
static int taksi, pamato;
private final Context myContext;
int jerbang;

public DataConn(Context context) {
super(context, DATABASE_NAME, null, 1);
// SQLiteDatabase db = this.getWritableDatabase();


this.myContext = context;
}

/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();

if(dbExist){
//do nothing - database already exist
}else{

//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");

}
}

}

/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){

SQLiteDatabase checkDB = null;

try{
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}catch(SQLiteException e){

//database does't exist yet.

}

if(checkDB != null){

checkDB.close();

}

return checkDB != null ? true : false;
}

/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{

//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;

//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public void openDataBase() throws SQLException {

//Open the database
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}
@Override
public synchronized void close() {

if(db != null)
db.close();

super.close();

}




@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+" (_id INTEGER, ctrlno INTEGER, prima INTEGER, keyword TEXT, val VARCHAR(MAX))");
db.execSQL("CREATE TABLE IF NOT EXISTS easy (_id INTEGER, ctrlno INTEGER, prima INTEGER, keyword TEXT, val VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS diff (_id INTEGER, ctrlno INTEGER, prima INTEGER, keyword TEXT, val VARCHAR);");
db.execSQL("CREATE TABLE IF NOT EXISTS player (_id INTEGER,pname VARCHAR, escr INTEGER, dscr INTEGER, hscr INTEGER, tscr INTEGER);");

db.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TABLE_NAME);
db.execSQL("drop table if exists easy");
onCreate(db);
}

public Cursor displayLeaderBoard(){
SQLiteDatabase db = this.getWritableDatabase();
String where = null;
String LB_TABLE = "player";
String[] LB_COLUMNS = new String[] {"_id","pname","escr","dscr","hscr","tscr"};
String LB_SORT = "tscr DESC";
Cursor lb = db.query(LB_TABLE, LB_COLUMNS,where,null,null,null,LB_SORT,null);
if(lb !=null){
lb.moveToFirst();
}
return lb;
}
}

附加:我不认为我的代码有问题,因为如果是的话,为什么它在虚拟机(API 22)中完美运行?是在我的AndroidManifest.xml里吗?或者Build.Gradle?中的东西

AndroidManifest.xml

package="com.example.hamira.wordmatchinggame">

<application
android:allowBackup="true"
android:icon="@mipmap/launcher"

android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<uses-sdk
android:targetSdkVersion="23"
android:minSdkVersion="7"
/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ingameinterface"
android:label="@string/app_name">
</activity>
<activity
android:name=".MenuView"
android:label="@string/app_name">
</activity>
<activity
android:name=".PlayerView"
android:label="@string/app_name">
</activity>
<activity
android:name=".LevelView"
android:label="@string/app_name">
</activity>
<activity
android:name=".ingamedifficult"
android:label="@string/app_name">
</activity>
<activity
android:name=".LeaderBoard"
android:label="@string/app_name">
</activity>

</application>

Build.Gradle(应用程序)

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "com.example.hamira.wordmatchinggame"
minSdkVersion 7
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

}

已编辑我删除了db.close() onCreate((SQLiteDatabase db) 方法并向其他游标方法返回另一个错误。

错误日志:

          android.database.sqlite.SQLiteException: no such column: stats (code 1): , while compiling: UPDATE player SET stats = '0'

光标方法:

public Cursor ClearRecentPlayer() {
db = this.getWritableDatabase();
Cursor c = db.rawQuery("UPDATE player SET stats = '0'", null);
c.moveToFirst();
c.close();
return c;
}

最佳答案

首先,我必须指出您的 SQLiteOpenHelper 有点奇怪。如果您使用此代码,您可能会遇到一些奇怪的问题。

我不知道您的错误的详细信息,但我很确定在 onCreate 中关闭数据库是不正确的。

我强烈建议您阅读以下内容:https://developer.android.com/training/basics/data-storage/databases.html ,并使用为您的项目编写了一个标准的SQLiteOpenHelper。这不会占用您太多时间。

关于你的另一个问题,我想你完成新标准SQLiteOpenHelper后就不会再面对它了。

希望这可以帮助你。

关于java - Android:虚拟显示器和真实电话问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com