Android错误:线程退出未捕获的异常(组= 0x40015560)

时间:2014-02-28 08:20:33

标签: android

我在创建第一个Android应用程序时在logcat上收到此错误。 02-28 11:07:49.547: W/dalvikvm(16532): threadid=1: thread exiting with uncaught exception (group=0x40015560)我真的迷失了,无法找到错误的位置。这是我的活动。

public class Database extends Activity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


             try {
        String destPath = "/data/data/" + getPackageName()
                + "/databases/grammer";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB(getBaseContext().getAssets().open("grammer"),
                    new FileOutputStream(destPath));
            Toast.makeText(Database.this, "File exists", Toast.LENGTH_SHORT)
            .show(); 
        }
    } 

     catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(Database.this, "Error File", Toast.LENGTH_SHORT)
                .show(); 
    } 
    catch (IOException e1) {
        e1.printStackTrace();
       Toast.makeText(Database.this, "Error IO", Toast.LENGTH_SHORT)
                .show();
    }

       DatabaseHandler db = new DatabaseHandler(this);

       Cursor cursor = db.getAllQuestions();
        if (cursor.moveToFirst())
        {
            do {
                DisplayRecord(cursor);    
            }
            while(cursor.moveToNext());
        }

        db.close();
    }

        public void CopyDB(InputStream inputstream, OutputStream outputstream)        
                throws IOException {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputstream.read(buffer))>0){
                outputstream.write(buffer, 0, length);
            }
            inputstream.close();
            outputstream.close();
        }

        public void DisplayRecord(Cursor cursor){
            Toast.makeText(this,
                    "id:" + cursor.getString(0) + "\n" +
                    "Title:" + cursor.getString(8) + "\n" +
                    "Question:" + cursor.getString(1) + "\n" +
                    "Answer1:" + cursor.getString(2) + "\n" +
                    "Answer2:" + cursor.getString(3) + "\n" +
                    "Answer3:" + cursor.getString(4) + "\n" +
                    "Answer4:" + cursor.getString(5) + "\n" +
                    "Answer:" + cursor.getString(6) + "\n" +
                    "Answer Text:" + cursor.getString(7),
                    Toast.LENGTH_SHORT).show();
        }    
     }

这是数据库处理程序类: -

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "grammer";

    // Contacts table name
    private static final String TABLE_QUESTIONS = "questions";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "questions";
    private static final String KEY_TITLE = "question_title";
    public static final String KEY_ANSWER1 = "answer1";
    public static final String KEY_ANSWER2 = "answer2";
    public static final String KEY_ANSWER3 = "answer3";
    public static final String KEY_ANSWER4 = "answer4";
    public static final String KEY_ANSWER = "answer";
    public static final String KEY_ANSWER_TEXT = "answer_text";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    // will not need. if needed will have to modify adding the question title and answer text fields
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + TABLE_QUESTIONS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_ANSWER1 + " TEXT," + KEY_ANSWER2 + " TEXT," + KEY_ANSWER3 + " TEXT," 
                + KEY_ANSWER4 + " TEXT," + KEY_ANSWER + " TEXT," + KEY_TITLE + " TEXT," + KEY_ANSWER_TEXT + " TEXT," +")";

        db.execSQL(CREATE_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     * @return 
     */

    // Adding new contact
     long addQuestion(String Question, String Answer1, String Answer2, String Answer3, String Answer4, String Answer, String Answer_Text, String Question_Title ) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME, Question); // question Name
        values.put(KEY_ANSWER1, Answer1); // answer
        values.put(KEY_ANSWER2, Answer2); // answer
        values.put(KEY_ANSWER3, Answer3); // answer
        values.put(KEY_ANSWER4, Answer4); // answer
        values.put(KEY_ANSWER, Answer); // answer
        values.put(KEY_ANSWER_TEXT, Answer_Text);
        values.put(KEY_TITLE, Question_Title);        
    }

     addtodb getQuestion(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

            Cursor cursor = db.query(TABLE_QUESTIONS, new String[] { KEY_ID,
                    KEY_NAME, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER, KEY_ANSWER_TEXT, KEY_TITLE }, KEY_ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            addtodb question = new addtodb(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));

            return question;
        }

     public Cursor getAllQuestions() {
            List<addtodb> QuestionList = new ArrayList<addtodb>();
            // Select All Query
            String selectQuery = "SELECT  * FROM " + TABLE_QUESTIONS;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    addtodb question = new addtodb();
                    question.setID(Integer.parseInt(cursor.getString(0)));
                    question.setName(cursor.getString(1));
                    question.setAnswer1(cursor.getString(2));
                    question.setAnswer2(cursor.getString(3));
                    question.setAnswer3(cursor.getString(4));
                    question.setAnswer4(cursor.getString(5));
                    question.setAnswer(cursor.getString(6));
                    question.setAnswerText(cursor.getString(7));
                    question.setTitle(cursor.getString(8));
                    // Adding contact to list
                    QuestionList.add(question);
                } while (cursor.moveToNext());
            }
            //System.out.print("Hello");

            // return contact list
        return cursor;
        //return QuestionList; to get the questions as a list    
        }

         // Updating single contact
        public int updateQuestion(addtodb question) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_NAME, question.getName());
            values.put(KEY_ANSWER, question.getAnswer1());
            values.put(KEY_ANSWER, question.getAnswer2());
            values.put(KEY_ANSWER, question.getAnswer3());
            values.put(KEY_ANSWER, question.getAnswer4());
            values.put(KEY_ANSWER, question.getAnswer());
            values.put(KEY_ANSWER_TEXT, question.getAnswerText());
            values.put(KEY_TITLE, question.getTitle());

            // updating row
            return db.update(TABLE_QUESTIONS, values, KEY_ID + " = ?",
                    new String[] { String.valueOf(question.getID()) });
        }

        // Deleting single contact
        public void deleteContact(addtodb question) {
            SQLiteDatabase db = this.getWritableDatabase();
            db.delete(TABLE_QUESTIONS, KEY_ID + " = ?",
                    new String[] { String.valueOf(question.getID()) });
            db.close();
        }

        // Getting contacts Count
        public int getContactsCount() {
            String countQuery = "SELECT  * FROM " + TABLE_QUESTIONS;
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            cursor.close();

            // return count
            return cursor.getCount();
        }            
}

我无法找到问题所在。

2 个答案:

答案 0 :(得分:3)

在您的代码中会产生问题甚至可能是异常的一个问题是:

+ KEY_TITLE + " TEXT," + KEY_ANSWER_TEXT + " TEXT," +")";
                                                 ^ ----- remove this ,

你不应该在')'之前有分号,否则你的表将不会被创建

答案 1 :(得分:2)

首先使用此

更正您的Create Table Query
String CREATE_TABLE = "CREATE TABLE " + TABLE_QUESTIONS + "("
            + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT,"
            + KEY_ANSWER1 + " TEXT, " + KEY_ANSWER2 + " TEXT, " + KEY_ANSWER3 + " TEXT, " 
            + KEY_ANSWER4 + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_TITLE + " TEXT, " + KEY_ANSWER_TEXT + " TEXT" +")";