2014-10-18 175 views
-2

我在代碼中沒有任何錯誤,但是當我運行應用程序時它崩潰。這是一張日誌圖片。 http://postimg.org/image/bbcovjovn/我無法弄清楚代碼中有什麼問題。Android應用程序在啓動運行時崩潰異常

MyBowlingScoresApplication.java

import java.util.ArrayList; 

import android.app.Application; 
import android.database.sqlite.SQLiteDatabase; 

public class MyBowlingScoresApplication extends Application { 

    private ArrayList<BowlingScores> allBowlingScores; 
private SQLiteDatabase bowlingScoresDB; 
    @Override 
    public void onCreate() { 
     super.onCreate(); 

     BowlingScoresDatabaseHelper databaseHelper1 = new BowlingScoresDatabaseHelper(this); 


     bowlingScoresDB = databaseHelper1.getWritableDatabase(); 

     //TODO get the data out of the database 
     allBowlingScores = new ArrayList<BowlingScores>(); 
    } 

    public void addBowlingScores(BowlingScores bowlingScores){ 
     assert bowlingScores != null; 

     allBowlingScores.add(bowlingScores); 
    } 

    public ArrayList<BowlingScores> getAllBowlingScores() { 
     return allBowlingScores; 
    } 

    private void setAllBowlingScores(ArrayList<BowlingScores> allBowlingScores) { 
     this.allBowlingScores = allBowlingScores; 
    } 


} 

BowlingScoresDatabaseHelper.java

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class BowlingScoresDatabaseHelper extends SQLiteOpenHelper { 



public static final int DB_VERSION = 1; 
public static final String DB_NAME = "MyBowlingScores.SQLite"; 
public static String BOWLING_SCORES_TABLE="BowlingScoresTable"; 
public static String RECORD_ID="ID"; 
public static String DATE = "Date"; 
public static String GAME1 = "Game1"; 
public static String GAME2 = "Game2"; 
public static String GAME3 = "Game3"; 



    public BowlingScoresDatabaseHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase bowlingScoresDB) { 
    String sqlStatement = "create table " + BOWLING_SCORES_TABLE 
      + " (" 
      + RECORD_ID + " integer primary key autoincrement not null," 
      + DATE + " integer," 
      + GAME1 + " integer," 
      + GAME2 + " integer," 
      + GAME3 + " integer," 
      + ");"; 


Log.d("Bowling Database", sqlStatement); 

    bowlingScoresDB.execSQL(sqlStatement); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

發表您的SQLite數據庫的輔助類 – Elltz 2014-10-18 17:31:11

+0

您已在數據庫創建粘貼兩個類相同......請發表您的BowlingScoresDatabaseHelper類 – Jamil 2014-10-18 17:31:29

+0

請發表您的logcat(不是圖片,而是實際的logcat) – 323go 2014-10-18 17:32:14

回答

4

SQLite的查詢語法是錯誤的:

create tableBowlingScoresTable... 

應該是這樣的:

create table BowlingScoresTable... 

忘記空間?

編輯:

好的,您現在已經提供了代碼。我看到的另一個語法錯誤是:

+ GAME3 + " integer,"最後不應該有逗號,因爲後面跟着)

+0

我不認爲這是問題。我更新上面,因爲我不小心貼同班兩次我的代碼。 – dfasfsd 2014-10-18 18:01:38

+0

@dfasfsd這確實是一個問題!它仍然崩潰? – 2014-10-18 18:33:14

1

我建議getting familiar with Android's wide set of debugging tools。你本來可以很容易地通過檢查堆棧跟蹤在logcat中,或查看調用層級時,當調試器附着發生崩潰找準你的問題的原因。

堆棧跟蹤清楚地說明了用於創建表的SQL語句包含無效的語法。我發現了兩個問題:

  1. A之後「創建表」丟失的空間,
  2. 最後一列定義後,一個意外的逗號。
相關問題