Eclipse - ERROR: "... cannot be resolved to a type."

  • Antworten:19
  • Bentwortet
Alexander P.
  • Forum-Beiträge: 16

25.01.2013, 20:24:52 via Website

Hi Liebe Community,
ich bin neue hier im Forum^^
und ich habe ein mittelschweres Problem.
Ich bin jetzt auch so langsam in die Android-Programmierung eingestiegen und bastel auch schon an meiner ersten App (mit eclipse) . Diese soll eine Datenbank enthalten. Ich habe es auch hingekriegt. Die Datenbank funktioniert und eine Speicherfunktion ist auch vorhanden. Die Speicherfunktion soll aber nur als Ergänzung dienen, dass also noch eigene Sachen zur Datenbank hinzufügt werden können.
Ich bin zuerst nach diesem Tutorial auf "vogella.com/articles/AndroidSQLite/article.html" gegangen um die DB zu erzeugen. Danach wollte ich die DB mit einigen Werten füllen (zum Test habe ich erstmal 3 genommen). Dazu habe ich diese Seite verwendet "reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/".
Ging alles soweit ganz gut. Bis auf einen Fehler. Der zweite Quelltextabschnitt aus dem 2. Link ist das Problem. Beim "DataBaseHelper myDbHelper = new DataBaseHelper();" bekomme ich den Error:"DataBaseHelper cannot be resolved to a type." Ich hab das ja bei mir etwas umbenannt, um das an meine App anzupassen, aber er kreidet mir immer noch bei "CocktailDatabaseHelper myDbHelper = new DataBaseHelper();" das zweite "DataBaseHelper" an und der ERROR bleibt:"DataBaseHelper cannot be resolved to a type." Ich habe mich durch x Internetseiten gelesen und weiß einfach nicht mehr weiter.
Ich hoffe daher, dass ihr mir weiterhelfen könnt. Danke schon mal im Voraus :)
PS: Sorry für die etwas längere Beschreibung ^^

Hier ist mein Quellcode:
1package de.android.app.cocktailbar.database;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7
8import de.android.app.cocktailbar.R;
9
10import android.content.Context;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15
16
17public class CocktailDatabaseHelper extends SQLiteOpenHelper {
18
19 //The Android's default system path of your application database.
20 private static String DB_PATH = "/data/data/de.android.app.cocktailbar.database/databases/";
21
22 private static String TABLE_COCKTAIL = "cocktail";
23
24 private SQLiteDatabase cocktailDB;
25
26 private final Context CocktailContext;
27
28 /**
29 * Constructor
30 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
31 * @param context
32 */
33 public CocktailDatabaseHelper(Context context) {
34
35 super(context, TABLE_COCKTAIL, null, 1);
36 this.CocktailContext = context;
37 }
38
39 /**
40 * Creates a empty database on the system and rewrites it with your own database.
41 * */
42 public void createDataBase() throws IOException{
43
44 boolean dbExist = checkDataBase();
45
46 if(dbExist){
47 //do nothing - database already exist
48 }else{
49
50 //By calling this method and empty database will be created into the default system path
51 //of your application so we are gonna be able to overwrite that database with our database.
52 this.getReadableDatabase();
53
54 try {
55
56 copyDataBase();
57
58 } catch (IOException e) {
59
60 throw new Error("Error copying database");
61
62 }
63 }
64
65 }
66
67 /**
68 * Check if the database already exist to avoid re-copying the file each time you open the application.
69 * @return true if it exists, false if it doesn't
70 */
71 private boolean checkDataBase(){
72
73 SQLiteDatabase checkDB = null;
74
75 try{
76 String myPath = DB_PATH + TABLE_COCKTAIL;
77 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
78
79 }catch(SQLiteException e){
80
81 //database does't exist yet.
82
83 }
84
85 if(checkDB != null){
86
87 checkDB.close();
88
89 }
90
91 return checkDB != null ? true : false;
92 }
93
94 /**
95 * Copies your database from your local assets-folder to the just created empty database in the
96 * system folder, from where it can be accessed and handled.
97 * This is done by transfering bytestream.
98 * */
99 private void copyDataBase() throws IOException{
100
101 //Open your local db as the input stream
102 InputStream myInput = CocktailContext.getAssets().open(TABLE_COCKTAIL);
103
104 // Path to the just created empty db
105 String outFileName = DB_PATH + TABLE_COCKTAIL;
106
107 //Open the empty db as the output stream
108 OutputStream myOutput = new FileOutputStream(outFileName);
109
110 //transfer bytes from the inputfile to the outputfile
111 byte[] buffer = new byte[1024];
112 int length;
113 while ((length = myInput.read(buffer))>0){
114 myOutput.write(buffer, 0, length);
115 }
116
117 //Close the streams
118 myOutput.flush();
119 myOutput.close();
120 myInput.close();
121
122 }
123
124 public void openDataBase() throws SQLException{
125
126 //Open the database
127 String myPath = DB_PATH + TABLE_COCKTAIL;
128 cocktailDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
129
130 }
131
132 @Override
133 public synchronized void close() {
134
135 if(cocktailDB != null)
136 cocktailDB.close();
137
138 super.close();
139
140 }
141
142 @Override
143 public void onCreate(SQLiteDatabase db) {
144
145 }
146
147 @Override
148 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
149
150 }
151
152 CocktailDatabaseHelper myDbHelper = new DataBaseHelper();
153 myDbHelper = new DataBaseHelper(this);
154
155 try {
156
157 myDbHelper.createDataBase();
158
159 } catch (IOException ioe) {
160
161 throw new Error("Unable to create database");
162
163 }
164
165 try {
166
167 myDbHelper.openDataBase();
168
169 }catch(SQLException sqle){
170
171 throw sqle;
172
173 }
174
175 }
176}

— geändert am 26.01.2013, 11:44:56

Antworten
Alexander P.
  • Forum-Beiträge: 16

26.01.2013, 11:03:01 via Website

Ja, das ist ja auch nur der "Beispiel-Quelltext" bei mir. Normalerweise steht für example was. Falls du verstehst, wie ich das meine. ^^ Ich kann den "originalen" Quelltext reinschreiben. Wollte erst nicht den originalen Quelltext reinschreiebn, aber jetzt steht im ersten Beitrag.
Sorry

EDIT: Jetzt ist der Quelltext so, wie er bei mir steht

— geändert am 26.01.2013, 11:09:20

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

26.01.2013, 12:52:54 via Website

DataBaseHelper cannot be resolved to a type.

Gleiche Antwort - nur in grün:

CocktailDatabaseHelper - Du hast die Klasse umbenannt. Wie soll er DataBaseHelper finden wenn die Klasse CocktailDatabaseHelper heißt. Und im Falle der Basisklasse: Achte auch mal auf die Klein- und Großschrift.

Antworten
Alexander P.
  • Forum-Beiträge: 16

26.01.2013, 14:15:50 via Website

Also soll ich das CocktailDatabaseHelper in DataBaseHelper umbennen? Oder das DataBaseHelper in CocktailDatabseHelper? Tut mir leid, ich hab noch nicht so viel Ahnung ^^
Benenne ich CocktailDatabaseHelper in DataBaseHelper um, sieht die Sache wie folgt aus:
1DataBaseHelper myDbHelper = new DataBaseHelper();
2 myDbHelper = new DataBaseHelper(this);
3
4 try {
5
6 myDbHelper.createDataBase();
7
8 } catch (IOException ioe) {
9
10 throw new Error("Unable to create database");
11
12 }
13
14 try {
15
16 myDbHelper.openDataBase();
17
18 }catch(SQLException sqle){
19
20 throw sqle;
21
22 }
23
24 }
Dann kommen folgende Fehler:
in Zeile 1:
Multiple markers at this line
- DataBaseHelper cannot be resolved to a type
- Syntax error on token ";", { expected after this token
- DataBaseHelper cannot be resolved to a type
in Zeile 6 und 16 nochmal das:
DataBaseHelper cannot be resolved to a type

Wenn ich es wie in meinem ersten Post habe, kommt nur in der ersten Zeile: Multiple markers at this line
- DataBaseHelper cannot be resolved to a type

Und wenn ich den zweten DataBaseHelper umbenne sieht die Sache wie folgt aus:
1CocktailDatabaseHelper myDbHelper = new CocktailDatabaseHelper();
2 myDbHelper = new DataBaseHelper(this);
3
4 try {
5
6 myDbHelper.createDataBase();
7
8 } catch (IOException ioe) {
9
10 throw new Error("Unable to create database");
11
12 }
13
14 try {
15
16 myDbHelper.openDataBase();
17
18 }catch(SQLException sqle){
19
20 throw sqle;
21
22 }
23
24 }
Dann kommen folgende Meldungen (alles in Zeile 1):
Multiple markers at this line
- The constructor CocktailDatabaseHelper() is undefined
- DataBaseHelper cannot be resolved to a type
- Syntax error on token ";", { expected after this token

Wie gesagt, ich hab noch nicht so viel Ahnung

EDIT:
Aaaah, so langsam dämmert's bei mir glaub ich. Habs jetzt so verstanden, dass ich alles "DataBaseHelper" in "CocktailDatabaseHelper" umbennen muss. Hab ich gemacht.

1CocktailDatabaseHelper cocktailDbHelper = new CocktailDatabaseHelper();
2 cocktailDbHelper = new CocktailDatabaseHelper(this);
3
4 try {
5
6 cocktailDbHelper.createDataBase();
7
8 } catch (IOException ioe) {
9
10 throw new Error("Unable to create database");
11
12 }
13
14 try {
15
16 cocktailDbHelper.openDataBase();
17
18 }catch(SQLException sqle){
19
20 throw sqle;
21
22 }
23
24
25 }

Der ERROR "DataBaseHelper cannot be resolved to a type." ist aus dem Spiel. Jetzt ist nur noch:
Multiple markers at this line
- Syntax error on token ";", { expected after this token
- The constructor CocktailDatabaseHelper() is undefined

Und da hört's bei mir schon wieder auf :unsure:

— geändert am 26.01.2013, 15:48:05

Antworten
Gelöschter Account
  • Forum-Beiträge: 694

26.01.2013, 19:59:15 via Website

Bei Basisklasse:

1DatabaseHelper db = new CocktailDatabaseHelper(...);

Ansonsten:

1CocktailDatabaseHelper db = new CocktailDatabaseHelper(...);

Du hattest DataBaseHelper statt DatabaseHelper geschrieben. Ich erwähnte deshalb die Groß-/Kleinschrift.

— geändert am 26.01.2013, 19:59:33

Antworten
Alexander P.
  • Forum-Beiträge: 16

26.01.2013, 20:10:17 via Website

Vielen Dank soweit für deine Hilfe. Aber ich seh da immer noch nicht ganz durch. Hab das jetzt so übernommen wie du das geschrieben hast. Aber die letzten beiden ERROR's
(Multiple markers at this line
- Syntax error on token ";", { expected after this token
- The constructor CocktailDatabaseHelper() is undefined)
bleiben. Muss da noch irgendwas in die Klammern rein? Auch das mit der geforderten geschweiften Klammer versteh ich nicht. Wenn ich eine solche setze und danach wieder eine zum Schließen mache, bleibt der Fehler weiterhin bzw. es kommen neue. Tut mir leid, aber ich steig da noch nicht 100%ig hinter...

Antworten
Gelöschter Account
  • Forum-Beiträge: 5.136

26.01.2013, 21:02:35 via Website

Du hast schlicht zuviel schließende geschweifte Klammern ... schau mal genau hin.

lg Voss

Antworten
Alexander P.
  • Forum-Beiträge: 16

26.01.2013, 21:45:11 via Website

Ja stimmt irgendwie schon aber wenn ich eine weg mache, krieg ich den ERROR, dass ich noch eine setzen muss, um "class body" zu beenden.
Aber irgendwie passt das, alle Klammern richtig, wenn ich so gucke, deswegen verstehe ich das nicht.
Also irgendwas haut mit den Klammern nicht hin.
Ohne den fehlerhaften Abschnitt sieht die Sache so aus:
1package de.android.app.cocktailbar.database;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7
8import de.android.app.cocktailbar.R;
9
10import android.content.Context;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15
16
17public class CocktailDatabaseHelper extends SQLiteOpenHelper {
18
19 //The Android's default system path of your application database.
20 private static String DB_PATH = "/data/data/de.android.app.cocktailbar.database/databases/";
21
22 private static String TABLE_COCKTAIL = "cocktail";
23
24 private SQLiteDatabase cocktailDB;
25
26 private final Context CocktailContext;
27
28
29 /**
30 * Constructor
31 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
32 * @param context
33 */
34 public CocktailDatabaseHelper(Context context) {
35
36 super(context, TABLE_COCKTAIL, null, 1);
37 this.CocktailContext = context;
38 }
39
40
41
42 /**
43 * Creates a empty database on the system and rewrites it with your own database.
44 * */
45 public void createDataBase() throws IOException{
46
47 boolean dbExist = checkDataBase();
48
49 if(dbExist){
50 //do nothing - database already exist
51 }else{
52
53 //By calling this method and empty database will be created into the default system path
54 //of your application so we are gonna be able to overwrite that database with our database.
55 this.getReadableDatabase();
56
57 try {
58
59 copyDataBase();
60
61 } catch (IOException e) {
62
63 throw new Error("Error copying database");
64
65 }
66 }
67
68 }
69
70 /**
71 * Check if the database already exist to avoid re-copying the file each time you open the application.
72 * @return true if it exists, false if it doesn't
73 */
74 private boolean checkDataBase(){
75
76 SQLiteDatabase checkDB = null;
77
78 try{
79 String myPath = DB_PATH + TABLE_COCKTAIL;
80 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
81
82 }catch(SQLiteException e){
83
84 //database does't exist yet.
85
86 }
87
88 if(checkDB != null){
89
90 checkDB.close();
91
92 }
93
94 return checkDB != null ? true : false;
95 }
96
97 /**
98 * Copies your database from your local assets-folder to the just created empty database in the
99 * system folder, from where it can be accessed and handled.
100 * This is done by transfering bytestream.
101 * */
102 private void copyDataBase() throws IOException{
103
104 //Open your local db as the input stream
105 InputStream myInput = CocktailContext.getAssets().open(TABLE_COCKTAIL);
106
107 // Path to the just created empty db
108 String outFileName = DB_PATH + TABLE_COCKTAIL;
109
110 //Open the empty db as the output stream
111 OutputStream myOutput = new FileOutputStream(outFileName);
112
113 //transfer bytes from the inputfile to the outputfile
114 byte[] buffer = new byte[1024];
115 int length;
116 while ((length = myInput.read(buffer))>0){
117 myOutput.write(buffer, 0, length);
118 }
119
120 //Close the streams
121 myOutput.flush();
122 myOutput.close();
123 myInput.close();
124
125 }
126
127 public void openDataBase() throws SQLException{
128
129 //Open the database
130 String myPath = DB_PATH + TABLE_COCKTAIL;
131 cocktailDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
132
133 }
134
135 @Override
136 public synchronized void close() {
137
138 if(cocktailDB != null)
139 cocktailDB.close();
140
141 super.close();
142
143 }
144
145 @Override
146 public void onCreate(SQLiteDatabase db) {
147
148 }
149
150 @Override
151 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
152
153 }
154
155 // Add your public helper methods to access and get content from the database.
156 // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
157 // to you to create adapters for your views.
158
159
160
161}
Hier wird mir kein Klammerfehler angezeigt.

Jetzt mit dem fehlerhaften Abschnitt:(ich verwende KEINE überflüssige Klammern. Jede die jetzt dazu kommt hat immer eine zweite, also für öffnen und schließen)
1package de.android.app.cocktailbar.database;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7
8import de.android.app.cocktailbar.R;
9
10import android.content.Context;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15
16
17public class CocktailDatabaseHelper extends SQLiteOpenHelper {
18
19 //The Android's default system path of your application database.
20 private static String DB_PATH = "/data/data/de.android.app.cocktailbar.database/databases/";
21
22 private static String TABLE_COCKTAIL = "cocktail";
23
24 private SQLiteDatabase cocktailDB;
25
26 private final Context CocktailContext;
27
28
29 /**
30 * Constructor
31 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
32 * @param context
33 */
34 public CocktailDatabaseHelper(Context context) {
35
36 super(context, TABLE_COCKTAIL, null, 1);
37 this.CocktailContext = context;
38 }
39
40
41
42 /**
43 * Creates a empty database on the system and rewrites it with your own database.
44 * */
45 public void createDataBase() throws IOException{
46
47 boolean dbExist = checkDataBase();
48
49 if(dbExist){
50 //do nothing - database already exist
51 }else{
52
53 //By calling this method and empty database will be created into the default system path
54 //of your application so we are gonna be able to overwrite that database with our database.
55 this.getReadableDatabase();
56
57 try {
58
59 copyDataBase();
60
61 } catch (IOException e) {
62
63 throw new Error("Error copying database");
64
65 }
66 }
67
68 }
69
70 /**
71 * Check if the database already exist to avoid re-copying the file each time you open the application.
72 * @return true if it exists, false if it doesn't
73 */
74 private boolean checkDataBase(){
75
76 SQLiteDatabase checkDB = null;
77
78 try{
79 String myPath = DB_PATH + TABLE_COCKTAIL;
80 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
81
82 }catch(SQLiteException e){
83
84 //database does't exist yet.
85
86 }
87
88 if(checkDB != null){
89
90 checkDB.close();
91
92 }
93
94 return checkDB != null ? true : false;
95 }
96
97 /**
98 * Copies your database from your local assets-folder to the just created empty database in the
99 * system folder, from where it can be accessed and handled.
100 * This is done by transfering bytestream.
101 * */
102 private void copyDataBase() throws IOException{
103
104 //Open your local db as the input stream
105 InputStream myInput = CocktailContext.getAssets().open(TABLE_COCKTAIL);
106
107 // Path to the just created empty db
108 String outFileName = DB_PATH + TABLE_COCKTAIL;
109
110 //Open the empty db as the output stream
111 OutputStream myOutput = new FileOutputStream(outFileName);
112
113 //transfer bytes from the inputfile to the outputfile
114 byte[] buffer = new byte[1024];
115 int length;
116 while ((length = myInput.read(buffer))>0){
117 myOutput.write(buffer, 0, length);
118 }
119
120 //Close the streams
121 myOutput.flush();
122 myOutput.close();
123 myInput.close();
124
125 }
126
127 public void openDataBase() throws SQLException{
128
129 //Open the database
130 String myPath = DB_PATH + TABLE_COCKTAIL;
131 cocktailDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
132
133 }
134
135 @Override
136 public synchronized void close() {
137
138 if(cocktailDB != null)
139 cocktailDB.close();
140
141 super.close();
142
143 }
144
145 @Override
146 public void onCreate(SQLiteDatabase db) {
147
148 }
149
150 @Override
151 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
152
153 }
154
155 // Add your public helper methods to access and get content from the database.
156 // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
157 // to you to create adapters for your views.
158
159 CocktailDatabaseHelper cocktailDbHelper = new CocktailDatabaseHelper();
160 cocktailDbHelper = new CocktailDatabaseHelper(this);
161
162 try {
163
164 cocktailDbHelper.createDataBase();
165
166 } catch (IOException ioe) {
167
168 throw new Error("Unable to create database");
169
170 }
171
172 try {
173
174 cocktailDbHelper.openDataBase();
175
176 } catch(SQLException sqle){
177
178 throw sqle;
179
180 }
181
182}
Und jetzt wird mir zu den 2 anderen ERROR's:
"Syntax error, insert "}" to complete ClassBody"

Also an den Klammern scheint es nicht zu liegen. Das scheint zu passen. Es bleibt, wenn ich es ohne die scheinbar überflüssige Klammer lasse, bei:
Multiple markers at this line
- Syntax error on token ";", { expected after this token
- The constructor CocktailDatabaseHelper() is undefined

Ich weiß echt nicht mehr weiter ...:unsure:

— geändert am 26.01.2013, 22:00:54

Antworten
Alexander P.
  • Forum-Beiträge: 16

26.01.2013, 22:17:41 via Website

Okay. Dann werde ich deine Rat mal befolgen und den kompletten Quelltext nochmal per Hand alles abtippen. Mal gucken, was draus wird. Melde mich dann nochmal. Danke erstmal soweit für eure Hilfe

— geändert am 26.01.2013, 22:37:55

Antworten
Alexander P.
  • Forum-Beiträge: 16

27.01.2013, 12:34:03 via Website

So. Ich hab mir jetzt nochmal die Zeit genommen, um nochmal alles abzutippen. Leider bleiben die ERROR weiterhin bestehen:
Multiple markers at this line
- Syntax error on token ";", { expected after this token
- The constructor CocktailDatabaseHelper() is undefined

Mein Code sieht jetzt so aus. Hab die ganzen Anmerkungen nicht mit übernommen.
1package de.android.app.cocktailbar.database;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7
8import android.content.Context;
9import android.database.SQLException;
10import android.database.sqlite.SQLiteDatabase;
11import android.database.sqlite.SQLiteException;
12import android.database.sqlite.SQLiteOpenHelper;
13
14public class CocktailDatabaseHelper extends SQLiteOpenHelper {
15 private static String DB_PATH = "/data/data/de.android.app.cocktailbar.database/databases/";
16 private static String TABLE_COCKTAIL ="cocktail";
17 private SQLiteDatabase CocktailDatenbank;
18 private final Context CocktailContext;
19
20 public CocktailDatabaseHelper(Context context) {
21 super(context, TABLE_COCKTAIL, null, 1);
22 this.CocktailContext = context;
23 }
24
25 public void createDataBase() throws IOException {
26 boolean dbExist = checkDataBase();
27
28 if(dbExist){
29 } else {
30 this.getReadableDatabase();
31
32 try {
33 copyDataBase();
34 } catch (IOException e) {
35 throw new Error("Error copying database");
36 }
37 }
38 }
39
40 private boolean checkDataBase(){
41 SQLiteDatabase checkDB = null;
42 try {
43 String myPath = DB_PATH + TABLE_COCKTAIL;
44 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
45 } catch(SQLiteException e){
46
47 }
48 if(checkDB != null){
49 checkDB.close();
50 }
51 return checkDB != null ? true : false;
52 }
53
54 private void copyDataBase() throws IOException{
55 InputStream myInput = CocktailContext.getAssets().open(TABLE_COCKTAIL);
56 String outFileName = DB_PATH + TABLE_COCKTAIL;
57 OutputStream myOutput = new FileOutputStream(outFileName);
58
59 byte[] buffer = new byte[1024];
60 int length;
61 while ((length = myInput.read(buffer))>0) {
62 myOutput.write(buffer, 0, length);
63 }
64
65 myOutput.flush();
66 myOutput.close();
67 myInput.close();
68
69 }
70
71 public void openDataBase() throws SQLException {
72 String myPath = DB_PATH + TABLE_COCKTAIL;
73 CocktailDatenbank = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
74 }
75
76 @Override
77 public synchronized void close() {
78 if(CocktailDatenbank != null)
79 CocktailDatenbank.close();
80
81 super.close();
82 }
83
84 @Override
85 public void onCreate(SQLiteDatabase db) {
86
87 }
88
89 @Override
90 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
91
92 }
93
94 CocktailDatabaseHelper cocktailDBHelper = new CocktailDatabaseHelper();
95 cocktailDBHelper = new CocktailDatabaseHelper(this);
96
97 try {
98 cocktailDBHelper.createDataBase();
99
100 } catch (IOException ioe) {
101 throw new Error("unable to create database");
102 }
103
104 try {
105 cocktailDBHelper.openDataBase();
106
107 } catch(SQLiteException sqle) {
108 throw sqle;
109 }
110}
111
112}
Beide Fehler sind in Zeile 94. Ich verstehe die einfach nicht. Wenn ich genau nachgucke, ist eine Klammer zum schließen zu viel. Mach ich die weg kommt folgende Meldung: "Syntax error, insert "}" to complete ClassBody": Aber eigentlich ist diese Klammer zu viel. Mach ich eine wieder hin, ist der Error weg. Ich weiß so langsam echt nicht mehr weiter. Kann mir nicht vorstellen, dass das so kompliziert ist. Der Quellcode meines Projektes funktioniert sonst einwandfrei. Was hat das mit dem "Constructor" auf sich. Wie definiere ich den?
Danke schonmal im Voraus für eure Hilfe :)

Antworten
Alexander P.
  • Forum-Beiträge: 16

27.01.2013, 13:16:28 via Website

So. Hab wieder ein bisschen rumprobiert. Auch das erstellen einer neuen Klasse, um dort den Quelltext reinzupacken. Und den "CocktailDatabaseHelper cocktailDbHelper" unter die "public class" Zeile zu packen, bleibt ohne Erfolg. App lädt aufs Handy, aber dann kommt "Leider wurde -AppName- beendet.".
Langsam zweifle ich daran, ob das überhaupt so geht, wie ich das machen will^^. Ich möchte ja eine Datenbank mit gefüllten Werten beim ersten Start der App haben, die dann in einer ListView angezeigt werden. Ich hab die Datenbank im Quelltext auch generiert, genau wie das in dem Tutorial von vogella (vogella.com/articles/AndroidSQLite/article.html#todo) erklärt ist.
Die gefüllte Datenbank (nach diesem Tutorial: reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications) hat die Feldernamen, wie meine im Quelltext.
Ich hab einfach nochmal die Links dazugetan. Kann das sein, dass das in der Kombination einfach nichts werden kann?

EDIT: Mir wurde vorgeschlagen bei "CocktailDatabaseHelper cocktailDBHelper = new CocktailDatabaseHelper();" in die Klammern "CocktailContext" reinzuschrieben, sodass da steht: CocktailDatabaseHelper cocktailDBHelper = new CocktailDatabaseHelper(CocktailContext);
Ich versteh das zwar nicht so ganz, aber der Error "- The constructor CocktailDatabaseHelper() is undefined" ist weg.
Jetzt bleibt aber weiterhin (nur) noch: "Syntax error on token ";", { expected after this token"
Gibt's dafür 'ne plausible Erklärung?

EDIT 2: Sämtliche ERROR's sind weg. YEAH :D. Nur gehts jetzt damit weiter, dass beim laden auf's Handy und dem damit verbundenen ersten Start der App, die Meldung kommt "Leider wurde -AppName- beendet". Dann wird die "log" relativ schnell mit Einträgen gefüllt. Nur sehe ich da nicht durch. Kann mir da einer weiterhelfen?
log:
01-27 17:09:54.107: E/HtcModeClient(343): Check connection and retry 3 times.
01-27 17:09:54.272: E/ExternalAccountType(9827): Unsupported attribute readOnly
01-27 17:09:55.067: E/ExternalAccountType(9827): Unsupported attribute readOnly
01-27 17:09:59.162: E/HtcModeClient(343): Check connection and retry 4 times.
01-27 17:09:59.542: E/wpa_supplicant(18165): Unsupported command: SETSUSPENDMODE 0
01-27 17:09:59.647: E/SQLiteLog(10404): (1) no such table: CocktailDatabase
01-27 17:09:59.792: E/AndroidRuntime(10404): FATAL EXCEPTION: AsyncTask #1
01-27 17:09:59.792: E/AndroidRuntime(10404): java.lang.RuntimeException: An error occured while executing doInBackground()
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.lang.Thread.run(Thread.java:864)
01-27 17:09:59.792: E/AndroidRuntime(10404): Caused by: android.database.sqlite.SQLiteException: no such table: CocktailDatabase (code 1): , while compiling: SELECT _id, name FROM CocktailDatabase
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:65)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1370)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:421)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:315)
01-27 17:09:59.792: E/AndroidRuntime(10404): at de.android.app.cocktailbar.contentprovider.MyCocktailContentProvider.query(MyCocktailContentProvider.java:77)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.ContentProvider.query(ContentProvider.java:673)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.ContentProvider$Transport.query(ContentProvider.java:210)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.ContentResolver.query(ContentResolver.java:375)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.CursorLoader.loadInBackground(CursorLoader.java:65)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.CursorLoader.loadInBackground(CursorLoader.java:43)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:301)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:68)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:56)
01-27 17:09:59.792: E/AndroidRuntime(10404): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-27 17:09:59.792: E/AndroidRuntime(10404): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-27 17:09:59.792: E/AndroidRuntime(10404): ... 4 more
01-27 17:09:59.802: E/EmbeddedLogger(439): App crashed! Process: de.android.app.cocktailbar
01-27 17:09:59.802: E/EmbeddedLogger(439): App crashed! Package: de.android.app.cocktailbar v1 (1.0)
01-27 17:09:59.807: E/EmbeddedLogger(439): Application Label: Cocktailbar
01-27 17:09:59.832: E/(10404): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
01-27 17:10:04.187: E/HtcModeClient(343): Check connection and retry 5 times.

Danke schonmal im Voraus :)

— geändert am 27.01.2013, 17:12:56

Antworten
Alexander P.
  • Forum-Beiträge: 16

27.01.2013, 20:47:54 via Website

Ja den Eintrag hab ich auch schon gefunden. Nur frage ich mich, welche Datenbank es ist. Ist es die mit gefüllten Werten, die ich einfügen möchte? Oder ist es die, die ich in meinem Projekt im Quelltext erzeuge? Macht für mich momentan alles nicht so ganz Sinn

Antworten
Alexander P.
  • Forum-Beiträge: 16

28.01.2013, 17:39:32 via Website

Aber warum? Wie mach ich das denn, dass er eine findet?

Antworten
Gelöschter Account
  • Forum-Beiträge: 5.136

28.01.2013, 19:22:35 via Website

Ich bin da ganz bei Harald,

lerne Java und versuche zu verstehen, was der Code den Du verwendest macht. Das geht unter anderem sehr gut dadurch, dass Du lernst das Logcat zu verwenden und selber Debug Ausgaben setzt, in denen Du Dir an bestimmten Stellen die Inhalten von Variablen ausgeben lässt. Das gepaart mit dem lesen von Anleitungen macht es Dir leichter zu verstehen, warum etwas nicht funktioniert oder ob bestimmte Code-Bestandteile überhaupt angesprungen werden. In Deinem Fall zum Beispiel createDataBase überhaupt ausgeführt wird ... usw.

Setz mal in jeder Methode eine Logausgabe .. wie z.B. Hier im onCreate, Hier im OnResume, Hier im createDataBase - DbExists = false usw. usf. ---

Das macht Dir den Ablauf Deiner App verständlich und vermittelt die Funktionsweise

lg Voss

Antworten
Alexander P.
  • Forum-Beiträge: 16

28.01.2013, 21:22:34 via Website

Ja wahrscheinlich habt ihr recht. Bedanke mich trotzdem bei euch für die Hilfe :)

— geändert am 28.01.2013, 21:22:40

Antworten