有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android应用程序在emulator中运行良好,但由于外部数据库的原因,无法在设备上运行

我发现我使用的外部数据库无法通过在activity(DataConn.java)中查询来访问

应用程序MainActivity.java的主菜单就是显示的那一个,但当我按下按钮,重定向到另一个布局活动ingameinterface.java时,它崩溃了。T^T 我该怎么解决这个问题?请帮忙

DataConn。爪哇

package com.example.hamira.wordmatchinggame;

import 安卓.content.Context;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;
import java.util.Random;


public class DataConn extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "db_vocabulary.db";
    private static final String DB_PATH = "data/data/com.exampasle.hamira.wordmatchinggame/databases/";
    private static final String TABLE_NAME = "tblwords";
    private SQLiteDatabase db;
    static int taksi, pamato, a;


    public DataConn(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public Cursor getAllData(){
        Random r = new Random();
        int patad = r.nextInt(41 - 0) + 1;
        pamato = patad;
        db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select " +
                "max(case when Key_Word = 'Questn' then Val end) Question,\n" +
                "max(case when Key_Word = 'Defi' then Val end) Definition,\n" +
                "max(case when Key_Word = 'Cc1' then Val end) Choice1,\n" +
                "max(case when Key_Word = 'Cc2' then Val end) Choice2,\n" +
                "max(case when Key_Word = 'Cc3' then Val end) Choice3,\n" +
                "max(case when Key_Word = 'Cc4' then Val end) Choice4,\n" +
                "max(case when Key_Word = 'Ans' then Val end) Answer,\n" +
                "max(case when Key_Word = 'Stats' then Val end) Status\n" +
                "from "+TABLE_NAME+" where prima = '"+pamato+"'", null);

        res.close();
        return res;
    }

    public Cursor getScore(){
        db = this.getWritableDatabase();
        Cursor sc = db.rawQuery("select crntscore from TblScore",null);
        return sc;
    }

    public Cursor UpdateScore(){
        taksi=ingameinterface.passScore;
        a = taksi+1;
        db = this.getWritableDatabase();
        Cursor us = db.rawQuery("update TblScore set crntscore='"+a+"'",null);
        us.moveToFirst();
        us.close();
        return us;
    }
    public Cursor DropPrima(){

        db = this.getWritableDatabase();
        Cursor udp = db.rawQuery("update TblWords set Val = '1' where CtrlNo = '8' " +
                "and Prima ='"+pamato+"'",null);
        udp.moveToFirst();
        udp.close();

        return udp;
    }
    public Cursor ResetPrima(){
        int a = ingameinterface.passPrima;
        db = this.getWritableDatabase();
        Cursor udp = db.rawQuery("update TblWords set Val = '0' where CtrlNo = '8' " +
                "and Prima ='"+a+"'",null);
        udp.moveToFirst();
        udp.close();

        return udp;
    }
    public Cursor ResetScore(){
        db = this.getWritableDatabase();
        Cursor us = db.rawQuery("update TblScore set crntscore='0'",null);
        us.moveToFirst();
        us.close();
        return us;
    }
}

主要活动。爪哇

package com.example.hamira.wordmatchinggame;

import 安卓.content.Intent;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ImageButton;
import 安卓.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Intent game;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        game = new Intent( MainActivity.this, ingameinterface.class);

        TextView btnstrt = (TextView) findViewById(R.id.btnStartGame);
        btnstrt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                startActivity(game);

            }

        });

        final View btnoff = (ImageButton) findViewById(R.id.imgBtnVoloff);
        final View btnon = (ImageButton) findViewById(R.id.imgBtnVolon);

        btnon.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (v == btnon) {
                    btnoff.setVisibility(View.VISIBLE);
                    btnon.setVisibility(View.GONE);
                    //TURN OFF AUDIO

                }
            }
        });
        btnoff.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (v == btnoff) {
                    btnoff.setVisibility(View.GONE);
                    btnon.setVisibility(View.VISIBLE);
                    //TURN ON AUDIO

                }
            }
        });


        TextView btnexit = (TextView) findViewById(R.id.btnQuit);
        btnexit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0){
                安卓.os.Process.killProcess(安卓.os.Process.myPid());
                System.exit(1);
            }
        });

    }
}

英格梅界面。爪哇

package com.example.hamira.wordmatchinggame;


import 安卓.app.Activity;
import 安卓.content.DialogInterface;
import 安卓.database.Cursor;
import 安卓.support.v7.app.AlertDialog;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import java.util.Random;

public class ingameinterface extends Activity {
    DataConn myDb;
    String  holdQstn, holdDefi, holdCc1, holdCc2, holdCc3, holdCc4,holdAns, holdStats;
    static int DBscore, passScore;
    static int patad, passPrima;
    TextView TVScore;
    TextView TVWord, TVDefi, TVCc1, TVCc2, TVCc3, TVCc4;
    Button ResetBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ingameinterface);

        myDb = new DataConn(this);
        CharSequence text = "GAME ON !";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(this, text, duration);
        toast.show();
        rand();
         hereWeGo();
        ShowMyScore();


        //BUTTON CHOICE 1
        TVCc1 = (TextView) findViewById(R.id.itmCc1);
        TVCc1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String BtnText1 = TVCc1.getText().toString();
                if(holdAns.equals(BtnText1)){
                    Toast.makeText(getApplicationContext(), BtnText1+" is the correct answer. " +
                            "Excellent!", Toast.LENGTH_SHORT).show();

                    performAddScore();
                    ShowMyScore();
                    dropPrima();
                    hereWeGo();
                }else
                {
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    hereWeGo();
                }
            }
        });
        //BUTTON CHOICE 2
        TVCc2 = (TextView) findViewById(R.id.itmCc2);
        TVCc2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String BtnText2 = TVCc2.getText().toString();
                if(holdAns.equals(BtnText2)){
                    Toast.makeText(getApplicationContext(), BtnText2+" is the correct answer. " +
                            "Excellent!", Toast.LENGTH_SHORT).show();

                    performAddScore();
                    ShowMyScore();
                    dropPrima();
                    hereWeGo();
                }else
                {
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    hereWeGo();
                }
            }
        });
        //BUTTON CHOICE 3
        TVCc3 = (TextView) findViewById(R.id.itmCc3);
        TVCc3.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String BtnText3 = TVCc3.getText().toString();
                if(holdAns.equals(BtnText3)){
                    Toast.makeText(getApplicationContext(), BtnText3+" is the correct answer. " +
                            "Excellent!", Toast.LENGTH_SHORT).show();

                    performAddScore();
                    ShowMyScore();
                    dropPrima();
                    hereWeGo();
                }else
                {
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    hereWeGo();
                }
            }
        });
        //BUTTON CHOICE 4
        TVCc4 = (TextView) findViewById(R.id.itmCc4);
        TVCc4.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String BtnText4 = TVCc4.getText().toString();
                if(holdAns.equals(BtnText4)){
                    Toast.makeText(getApplicationContext(), BtnText4+" is the correct answer. " +
                            "Excellent!", Toast.LENGTH_SHORT).show();

                    performAddScore();
                    ShowMyScore();
                    dropPrima();
                    hereWeGo();
                }else
                {
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    hereWeGo();
                }
            }
        });

        ResetBtn = (Button) findViewById(R.id.btnReset);
        ResetBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                AlertDialog.Builder build = new AlertDialog.Builder(ingameinterface.this);
                build.setTitle("Reset Confirmation");
                build.setMessage("Do you want to reset the game?");
                build.setPositiveButton("YES", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //BUTTON YES
                        for(int a=1; a!=41; a++){
                            passPrima = a;
                            myDb.ResetPrima();
                        }
                        myDb.ResetScore();
                        hereWeGo();
                        ShowMyScore();
                        Toast.makeText(getApplicationContext(), "Game reset successfully!", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                build.setNegativeButton("NO", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //BUTTON NO
                        Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                AlertDialog wew = build.create();
                wew.show();

            }
        });


    }//onCreate END
public void rand(){
    Random r = new Random();
    patad = r.nextInt(41 - 0) + 1;
}

    public  void hereWeGo(){

        Cursor res = myDb.getAllData();
        if(res.getCount() == 0){
            Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
            return;
        }
        while (res.moveToNext()){
            holdQstn = String.valueOf(res.getString(0));
            holdDefi = String.valueOf(res.getString(1));
            holdCc1 = String.valueOf(res.getString(2));
            holdCc2 = String.valueOf(res.getString(3));
            holdCc3 = String.valueOf(res.getString(4));
            holdCc4 = String.valueOf(res.getString(5));
            holdAns = String.valueOf(res.getString(6));
            holdStats = String.valueOf(res.getString(7));
            if (holdStats.equals("1")){
                reRollCall();
            }else{
                TVWord= (TextView) findViewById(R.id.itmWORD);
                String StrngToTVQstn = holdQstn;
                TVWord.setText("'"+StrngToTVQstn+"'");
                TVDefi= (TextView) findViewById(R.id.itmDefi);
                String StrngToTVDefi = holdDefi;
                TVDefi.setText(StrngToTVDefi);
                TVCc1= (TextView) findViewById(R.id.itmCc1);
                String StrngToTVCc1 = holdCc1;
                TVCc1.setText(StrngToTVCc1);
                TVCc2= (TextView) findViewById(R.id.itmCc2);
                String StrngToTVCc2 = holdCc2;
                TVCc2.setText(StrngToTVCc2);
                TVCc3= (TextView) findViewById(R.id.itmCc3);
                String StrngToTVCc3 = holdCc3;
                TVCc3.setText(StrngToTVCc3);
                TVCc4= (TextView) findViewById(R.id.itmCc4);
                String StrngToTVCc4 = holdCc4;
                TVCc4.setText(StrngToTVCc4);
            }
        }

    }
    public void reRollCall(){
        hereWeGo();
    }

    public void performAddScore(){
        Random r = new Random();
        patad = r.nextInt(41 - 0) + 1;
        myDb.UpdateScore();

    }
    public void ShowMyScore(){
        Cursor sc = myDb.getScore();
        while (sc.moveToNext()){
            DBscore= Integer.valueOf(sc.getInt(0));
            passScore = DBscore;
            TVScore= (TextView) findViewById(R.id.itmSCORE);
            TVScore.setText(String.valueOf(passScore));

        }
    }
    public void dropPrima(){
        myDb.DropPrima();

    }
}//class END

编辑:抱歉!粘贴了错误的日志

更新日志

    08-02 11:56:54.418 17223-17223/com.example.hamira.wordmatchinggame E/SQLiteLog: (1) no such table: tblwords 08-02 11:56:54.419 17223-17223/com.example.hamira.wordmatchinggame D/AndroidRuntime: Shutting down VM 08-02 11:56:54.420 17223-17223/com.example.hamira.wordmatchinggame E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hamira.wordmatchinggame, PID: 17223
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hamira.wordmatchinggame/com.example.hamira.wordmatchinggame.ingameinterface}: 安卓.database.sqlite.SQLiteException: no such table: tblwords (code 1): , while compiling: select max(case when Key_Word = 'Questn' then Val end) Question,
max(case when Key_Word = 'Defi' then Val end) Definition,
max(case when Key_Word = 'Cc1' then Val end) Choice1,
max(case when Key_Word = 'Cc2' then Val end) Choice2,
max(case when Key_Word = 'Cc3' then Val end) Choice3,
max(case when Key_Word = 'Cc4' then Val end) Choice4,
max(case when Key_Word = 'Ans' then Val end) Answer,
max(case when Key_Word = 'Stats' then Val end) Status
from tblwords where prima = '8'
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2525)
    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2617)
    at 安卓.app.ActivityThread.access$800(ActivityThread.java:182)
    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
    at 安卓.os.Handler.dispatchMessage(Handler.java:111)
    at 安卓.os.Looper.loop(Looper.java:218)
    at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:990)
    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:785)
 Caused by: 安卓.database.sqlite.SQLiteException: no such table: tblwords (code 1): , while compiling: select max(case when Key_Word = 'Questn' then Val end) Question,
max(case when Key_Word = 'Defi' then Val end) Definition,
max(case when Key_Word = 'Cc1' then Val end) Choice1,
max(case when Key_Word = 'Cc2' then Val end) Choice2,
max(case when Key_Word = 'Cc3' then Val end) Choice3,
max(case when Key_Word = 'Cc4' then Val end) Choice4,
max(case when Key_Word = 'Ans' then Val end) Answer,
max(case when Key_Word = 'Stats' then Val end) Status
from tblwords where prima = '8'
    at 安卓.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at 安卓.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
    at 安卓.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
    at 安卓.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at 安卓.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at 安卓.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
    at 安卓.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
    at 安卓.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
    at 安卓.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1285)
    at com.example.hamira.wordmatchinggame.DataConn.getAllData(DataConn.java:38)
    at com.example.hamira.wordmatchinggame.ingameinterface.hereWeGo(ingameinterface.java:167)
    at com.example.hamira.wordmatchinggame.ingameinterface.onCreate(ingameinterface.java:37)
    at 安卓.app.Activity.performCreate(Activity.java:6135)
    at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2472)
        ... 10 more 08-02 11:56:54.474 17223-17223/com.example.hamira.wordmatchinggame I/Process: Sending signal. PID: 17223 SIG: 9

共 (0) 个答案