2013-04-01 196 views
0

我試圖連接我使用SQLite創建的數據庫。我沒有任何錯誤,但每當我嘗試在模擬器上運行該應用程序,應用程序自動停止工作,只要它開始..繼承人代碼只有一個活動,連接數據庫並從中提取數據並顯示它停止Android應用程序

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.sampledb" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 
<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.sampledb.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> 
    </application> 
    </manifest> 

繼承人具有一個TextView並且其中文本是從數據庫extraxted一個無線電設備組的佈局文件:

XML:在下面的XML文件我已經創建的文本視圖,其中我想把問題和單選按鈕及其文本放在我想要做出選擇的地方。繼承人的代碼。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainScreen" > 

    <TextView 
     android:id="@+id/question" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="sample" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"> 

    <RadioGroup 
     android:id="@+id/rg1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      /> 

     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

      <RadioButton 
      android:id="@+id/radio3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 
    </RadioGroup> 

    </LinearLayout> 

    <TextView 
     android:id="@+id/explanation" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Explanation" /> 
    </LinearLayout> 

在數據庫中,我有我試圖用它作爲單選按鈕文本,有一個問題很少coloumns和很少的選擇。 活動代碼:

package com.example.sampledb; 

import java.util.Locale; 
import com.example.sampledb.R; 
import android.os.Bundle; 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.view.Menu; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
public class MainActivity extends Activity { 
    TextView Question; 
    RadioGroup G1; 
    RadioButton B1, B2, B3, B4; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Question= (TextView)findViewById(R.id.question); 
     G1=(RadioGroup)findViewById(R.id.rg1); 
     B1=(RadioButton)findViewById(R.id.radio0); 
     B2=(RadioButton)findViewById(R.id.radio1); 
     B3=(RadioButton)findViewById(R.id.radio2); 
     B4=(RadioButton)findViewById(R.id.radio3); 
      SQLiteDatabase db; 
      db = openOrCreateDatabase("vocablearner.db", 
       SQLiteDatabase.CREATE_IF_NECESSARY, null); 
     db.setVersion(1); 
     db.setLocale(Locale.getDefault()); 


     Cursor cur = db.query("MainTable", null, null, null, null, null,null); 
     if (cur != null) { 
       if (cur.moveToFirst()) { 
         do { 
    String Quest = cur.getString(cur.getColumnIndex("_id")); 
    String ans1 = cur.getString(cur.getColumnIndex("C1")); 
    String ans2 = cur.getString(cur.getColumnIndex("C2")); 
    String ans3 = cur.getString(cur.getColumnIndex("C3")); 
    String ans4 = cur.getString(cur.getColumnIndex("C4")); 
    //String crt = cur.getString(cur.getColumnIndex("Ccrt")); 
    Question.setText(Quest); 
    B1.setText(ans1); 
    B2.setText(ans2); 
    B3.setText(ans3); 
    B4.setText(ans4); 

         }while (cur.moveToNext()); } } } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } }  ` 

在這裏,我已經使用的方法openorcreatedatabase定義我保存在資產文件夾中的數據庫和我已經使用光標來提取數據。

在數據庫文件中,_id作爲主鍵(問題),而coloumns c1 c2 c3 c4是答案。

我應該做些什麼改變來運行這段代碼?我應該上傳數據庫內容嗎?並有任何其他方式來使用Android中的數據庫?

這裏我使用了一個方法openorcreatedatabase來定義我保存在資產文件夾中的數據庫,我用光標來提取數據。

在數據庫文件中,_id作爲主鍵(問題),而coloumns c1 c2 c3 c4是答案。

我應該做些什麼改變來運行這段代碼?我應該上傳數據庫內容嗎?並有任何其他方式來使用Android中的數據庫?

+2

發表你的logcat – 323go

+1

@Arjun我知道在你的世界裏,通常用一些間距開始一個新的段落,但是它會完全混淆SO上的格式化程序。所以下一次:不要這樣做,只需從行首開始寫。 – WarrenFaith

+0

你有沒有考慮過使用SqliteOpenHelper()?它肯定會幫助清理你的onCreate()方法。另外,考慮在後臺線程中執行數據庫操作。 Vogella有一個很好的教程:http://www.vogella.com/articles/AndroidSQLite/article.html –

回答

-1

檢查您是否需要在清單中添加任何權限。就像寫入你的外部存儲器一樣。

+0

這不是一個答案。它應該是一個評論。 – Simon

+0

@Arjun Viswanathan它解決了你的問題? – Sourav048

+0

我已經更改了清單中的權限,並且我學會了以正確的方式使用query()方法..非常感謝所有這些幫助初學者:) – Sakthi