Ich fand heraus, dass die externe Datenbank, die ich benutze, nicht durch Abfragen in Aktivität (DataConn.java) zugreifen kann.Android App läuft perfekt im Emulator, aber nicht auf dem Gerät aufgrund externer DB
das Hauptmenü der App MainActivity.java
ist das eine zeigte, aber wenn ich den Knopf mit dem Effekt der Umleitung auf ein anderes Layout Aktivität ingameinterface.java
drücken, stürzt es ab. T^T Wie kann ich das beheben? bitte hilfe!
DataConn.java
package com.example.hamira.wordmatchinggame;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.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;
}
}
MainActivity.java
package com.example.hamira.wordmatchinggame;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.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){
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
}
ingameinterface.java
package com.example.hamira.wordmatchinggame;
import android.app.Activity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.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
EDIT: Entschuldigung! falscher logcat eingefügt.
AKTUALISIERT logcat
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}: android.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 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: 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 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.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 android.app.Activity.performCreate(Activity.java:6135)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.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
Es ist nicht klar, was Sie mit "externe Datenbank" meinen, weil Sie nur eine lokale Sqlite-Datenbank verwenden –