Android:虚拟监视器和Real Phone问题

时间:2017-02-25 03:09:02

标签: java android

我昨晚遇到一个严重的问题,这个项目(使用外部数据库)在虚拟监视器中运行得非常好

  

API 22,Android 1.5(Google API)

但如果我在手机上跑,

  

(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;
    }
}

其他 我不认为我的代码有问题,因为如果是,那么为什么它在VM(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(app)

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:+'

}

EDITED 我删除onCreate((SQLiteDatabase db)方法上的db.close()并将另一个错误返回给其他游标方法。

错误日志:

          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;
}

1 个答案:

答案 0 :(得分:0)

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

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

我强烈建议您阅读:https://developer.android.com/training/basics/data-storage/databases.html,并使用为您的项目编写标准SQLiteOpenHelper。这不会占用你的大部分时间。

关于您的另一个问题,我认为在您完成新标准SQLiteOpenHelper后,您将不会面对此问题。

希望这可以帮到你。