2014-10-22 16 views
1

我想開發一個應用程序,我需要在Android中使用SQLite數據庫。下面是顯示的代碼,它正在實現數據庫以及我的應用程序隨機數據所需的所有操作(只是爲了首先成功實現邏輯)。沒有這樣的表 - 在SQLite中插入錯誤的值Android

此代碼寫入DBHelperDisplay.java,它由MainActivity.java通過intent調用。

我DBHelperDisplay.java:

package course.examples.jumboquest; 

import java.util.Random; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
//import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
//import android.content.Intent; 
import android.os.CountDownTimer; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseUtils; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase; 

public class DBHelperDisplay extends ActionBarActivity { 

TextView tv; 
DBHelper myDB; 
RadioGroup radioChoices; 
RadioButton rbtChoice; 
Button btSubmit; 
String choice1; 
String choice2; 
String choice3; 
String choice4; 
String strAns; 
CustomTimer cdt; 

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

    cdt = new CustomTimer(20000, 1000); 
    cdt.start(); 

    myDB = new DBHelper(this); 

    myDB.insertQuestion(1, "who is the team member whose name starts with s?", "Vinita", "Akanksha", "Swati", "Megha", "Swati"); 
    myDB.insertQuestion(2, "who is the team member whose name starts with m?", "Vinita", "Akanksha", "Swati", "Megha", "Megha"); 
    myDB.insertQuestion(3, "who is the team member whose name starts with a?", "Vinita", "Akanksha", "Swati", "Megha", "Akanksha"); 
    myDB.insertQuestion(4, "who is the team member whose name starts with v?", "Vinita", "Akanksha", "Swati", "Megha", "Vinita"); 
    myDB.insertQuestion(5, "who is the team member whose name ends with i?", "Vinita", "Akanksha", "Swati", "Megha", "Swati"); 

    Cursor rs = myDB.getData(); 
    String Question = rs.getString(rs.getColumnIndex(DBHelper.Col_Ques)); 
    choice1 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice1)); 
    choice2 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice2)); 
    choice3 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice3)); 
    choice4 = rs.getString(rs.getColumnIndex(DBHelper.Col_Choice4)); 
    strAns = rs.getString(rs.getColumnIndex(DBHelper.Col_Ans)); 


    tv = (TextView) findViewById(R.id.timertxt); 

    final TextView quest = (TextView) findViewById(R.id.quest); 
    quest.setText(Question); 

    //final TextView ans = (TextView) findViewById(R.id.ans); 

    Button btClear = (Button)findViewById(R.id.btClear); 
    btClear.setText("CLEAR"); 

    addListenerRadioChoices() ; 

    btClear.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) { 
     //ans.setText(""); 

     } 
    }); 
} 
    public void addListenerRadioChoices(){ 

    radioChoices = (RadioGroup) findViewById(R.id.radioChoices); 

    ((RadioButton) radioChoices.getChildAt(0)).setText(choice1); 
    ((RadioButton) radioChoices.getChildAt(1)).setText(choice2); 
    ((RadioButton) radioChoices.getChildAt(2)).setText(choice3); 
    ((RadioButton) radioChoices.getChildAt(3)).setText(choice4); 

    btSubmit = (Button)findViewById(R.id.btSubmit); 
    btSubmit.setText("SUBMIT"); 
    btSubmit.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) { 

     int selected = radioChoices.getCheckedRadioButtonId(); 
     rbtChoice = (RadioButton) findViewById(selected); 
     String Ans = rbtChoice.getText().toString(); 
     if(Ans.equalsIgnoreCase(strAns)){ 

      cdt.cancel(); 
      //ans.setText(" ANSWER"); 

     } 
     /* else{ 
      // ans.setText("WRONG ANSWER"); 
     }*/ 

    } 
    }); 
    } 

public class CustomTimer extends CountDownTimer{ 
    //TextView ed; 
    public CustomTimer(long millisInFuture, long countDownInterval){ 
     super(millisInFuture, countDownInterval); 
    } 
    @Override 
    public void onTick(long millisUntilFinished){ 
     //current = millisUntilFinished/1000; 
     tv.setText("Time Left:" + millisUntilFinished/1000); 
    } 
    @Override 
    public void onFinish() { 
     tv.setText("Time Up - lost the game!"); 
    } 
} 

public class DBHelper extends SQLiteOpenHelper { 

    public static final String Database_Name = "Questions.db"; 
    public static final String Table_Name = "Comics"; 
    public static final String Col_ID = "id"; 
    public static final String Col_Ques = "question"; 
    public static final String Col_Choice1 = "choice1"; 
    public static final String Col_Choice2 = "choice2"; 
    public static final String Col_Choice3 = "choice3"; 
    public static final String Col_Choice4 = "choice4"; 
    public static final String Col_Ans = "answer"; 

    public DBHelper(Context context){ 
     super(context, Database_Name, null, 1); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db){ 
     // TODO Auto-generated method stub 
     db.execSQL(
     "CREATE TABLE Comics" + 
     "(id integer primary key, question text, choice1 text, choice2 text, choice3 text, choice4 text, answer text)"); 

    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("DROP TABLE IF EXISTS Comics"); 
      onCreate(db); 
    } 

    public boolean insertQuestion(int id, String question, String choice1, String choice2, String choice3, String choice4, String answer){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 

     contentValues.put("id",id); 
     contentValues.put("question",question); 
     contentValues.put("choice1",choice1); 
     contentValues.put("choice2",choice2); 
     contentValues.put("choice3",choice3); 
     contentValues.put("choice4",choice4); 
     contentValues.put("answer",answer); 
     db.insert("Comics", null, contentValues); 
     db.close(); 
     return true; 
    } 
    public Cursor getData(){ 
     SQLiteDatabase db = this.getReadableDatabase(); 
     int numRows = (int)DatabaseUtils.queryNumEntries(db,Table_Name); 
     int min = 1; 
     int max = numRows; 
     Random r = new Random(); 
     int id = r.nextInt(max - min + 1) + min; 
     Cursor res= db.rawQuery("Select * from Comics where id = " + id + "", null); 
     return res; 
    } 
} 

}

我的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="course.examples.jumboquest" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="21" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".DBHelperDisplay"></activity> 
</application> 

</manifest> 

在執行上面的代碼,我收到以下錯誤:

logcat file

我已經搜索了很多,發現許多堆棧溢出鏈接有相同的問題,但仍然沒有解決。我是這個主題的新手。請幫幫我。

logCat file2

+1

卸載並重新安裝應用程序,以擺脫可能的舊版本的數據庫文件。 http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto 2014-10-22 10:29:23

+1

在卸載之前,請確保清除數據。 :) – 2014-10-22 10:32:36

+1

@MysticMagic卸載也許是清除數據最直接的方法。僅清除數據也是可行的,但需要更長時間的解釋。 – laalto 2014-10-22 10:34:26

回答

3

我有兩個適配器:

公共類DBController_for_drivinglicense擴展SQLiteOpenHelper {

public DBController_for_drivinglicense(Context applicationcontext) { 
    super(applicationcontext,"nozadb", null, 1); 

} 
//Creates Table 
@Override 
public void onCreate(SQLiteDatabase database) { 

    String query; 
    query = "CREATE TABLE if not exists drivinglicense (id INTEGER PRIMARY KEY, comment TEXT,rates float,names TEXT,date DEFAULT CURRENT_TIMESTAMP, udpateStatus TEXT)"; 
    database.execSQL(query); 
} 

第一適配器是:

public DBController_for_landregistration(Context applicationcontext) { 
    super(applicationcontext,"nozadb", null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase database) { 
    String query; 

    query = "CREATE TABLE if not exists landregistration (id INTEGER PRIMARY KEY, comment TEXT,rates float,names TEXT,date DEFAULT CURRENT_TIMESTAMP, udpateStatus TEXT)"; 
    database.execSQL(query); 

} 

當我改變了第二個到:

公共類DBController_for_drivinglicense擴展SQLiteOpenHelper {

public DBController_for_drivinglicense(Context applicationcontext) { 
    super(applicationcontext,"nozadb", null, 2); 
} 
//Creates Table 
@Override 
public void onCreate(SQLiteDatabase database) { 

    String query; 
    query = "CREATE TABLE if not exists drivinglicense (id INTEGER PRIMARY KEY, comment TEXT,rates float,names TEXT,date DEFAULT CURRENT_TIMESTAMP, udpateStatus TEXT)"; 
    database.execSQL(query); 

} 
+0

非常感謝,它工作:)。但現在它顯示NullPointerException。我不知道爲什麼。你能否再次通過我的代碼? – 2014-10-22 10:53:23

+1

nullpointerexception在哪一行出現? – nunoh123 2014-10-22 10:57:04

+0

我已經上傳了logcat文件的圖片。我也在努力尋找自己。如果你能解決它,請讓我知道。 – 2014-10-22 11:05:29