2013-04-01 199 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