dimanche 27 mars 2016

Unable to resume activity, no such table: infteh (code 1): , while compiling: select * from infteh

Im absolutely new in programming for android. But I would love to get answers about my mistakes to develop myself. Of course Ive already watched the same questions about my mistakes, but they didn`t help me. My situation is: I try to make a live search from SQLite db using edittext and listview. There are activity_main (RelativeLayout) and activity_infteh (LinearLayout) where user goes pushing a button. The problem is my app deads after pushing this button, it tells it stopped. I have a database made in DB Browser for SQLite with a table called "infteh" in assets folder.

enter image description here

This is my logcat:

03-27 20:55:45.227    6418-6418/ru.aioa.itwords E/SQLiteLog﹕ (1) no such table: infteh
03-27 20:55:45.237    6418-6418/ru.aioa.itwords D/AndroidRuntime﹕ Shutting down VM
03-27 20:55:45.247    6418-6418/ru.aioa.itwords E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ru.aioa.itwords, PID: 6418
    java.lang.RuntimeException: Unable to resume activity {ru.aioa.itwords/ru.aioa.itwords.InftehActivity}: android.database.sqlite.SQLiteException: no such table: infteh (code 1): , while compiling: select * from infteh
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3544)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3575)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
            at android.app.ActivityThread.access$900(ActivityThread.java:181)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6220)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: android.database.sqlite.SQLiteException: no such table: infteh (code 1): , while compiling: select * from infteh
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1454)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1393)
            at ru.aioa.itwords.InftehActivity.onResume(InftehActivity.java:46)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
            at android.app.Activity.performResume(Activity.java:6495)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3533)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3575)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895)
            at android.app.ActivityThread.access$900(ActivityThread.java:181)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6220)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
03-27 20:55:48.797    6418-6418/ru.aioa.itwords I/Process﹕ Sending signal. PID: 6418 SIG: 9

This is my activity_infteh.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Информационные технологии"
        android:id="@+id/textView3"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Введите слово"
        android:id="@+id/inftehFilter" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/inftehList"
        android:focusable="true"
        android:focusableInTouchMode="true"/>

    </LinearLayout>

This is my DatabaseHelper.java:

package ru.aioa.itwords;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.database.sqlite.SQLiteDatabase;
    import android.content.Context;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileOutputStream;
    import java.io.File;
    import java.sql.SQLException;

    public class DatabaseHelper extends SQLiteOpenHelper {

        private static String DB_PATH = "//data/data/ru.aioa.itwords/databases/";
        private static String DB_NAME = "itwordsdb";
        private static final int SCHEMA = 1; 
        static final String TABLE = "infteh";

        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_WORD = "word";
        public static final String COLUMN_READING = "reading";
        public static final String COLUMN_DESCRIPTION = "description";
        public SQLiteDatabase database;
        private Context myContext;

        public DatabaseHelper(Context context) {
            super(context, DB_NAME, null, SCHEMA);
            this.myContext=context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,  int newVersion) {

        }

        public void create_db(){
            InputStream myInput = null;
            OutputStream myOutput = null;
            try {
                File file = new File(DB_PATH + DB_NAME);
                if (!file.exists()) {
                    this.getReadableDatabase();

                    myInput = myContext.getAssets().open(DB_NAME);

                    String outFileName = DB_PATH + DB_NAME;


                    myOutput = new FileOutputStream(outFileName);


                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = myInput.read(buffer)) > 0) {
                        myOutput.write(buffer, 0, length);
                    }

                    myOutput.flush();
                    myOutput.close();
                    myInput.close();
                }
            }
            catch(IOException ex){

            }
        }
        public void open() throws SQLException {
            String path = DB_PATH + DB_NAME;
            database = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READWRITE);

        }

        @Override
        public synchronized void close() {
            if (database != null) {
                database.close();
            }
            super.close();
        }}

This is my InftehActivity.java:

package ru.aioa.itwords;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
import java.sql.SQLException;



public class InftehActivity extends Activity {



    ListView inftehList;
    EditText inftehFilter;
    DatabaseHelper sqlHelper;
    Cursor userCursor;
    SimpleCursorAdapter userAdapter;

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


        inftehList = (ListView)findViewById(R.id.inftehList);
        inftehFilter = (EditText)findViewById(R.id.inftehFilter);
        sqlHelper = new DatabaseHelper(getApplicationContext());

        sqlHelper.create_db();
    }
    @Override
    public void onResume(){
        super.onResume();
        try {
            sqlHelper.open();
            userCursor = sqlHelper.database.rawQuery("select * from " + DatabaseHelper.TABLE, null);
            String[] headers = new String[]{DatabaseHelper.COLUMN_WORD, DatabaseHelper.COLUMN_READING, DatabaseHelper.COLUMN_DESCRIPTION};
            userAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
                    userCursor, headers, new int[]{android.R.id.text1, android.R.id.text2}, 0);


            if(!inftehFilter.getText().toString().isEmpty())
                userAdapter.getFilter().filter(inftehFilter.getText().toString());


            inftehFilter.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    userAdapter.getFilter().filter(s.toString());
                }
            });

            /
            userAdapter.setFilterQueryProvider(new FilterQueryProvider() {
                @Override
                public Cursor runQuery(CharSequence constraint) {

                    if (constraint == null || constraint.length() == 0) {

                        return sqlHelper.database.rawQuery("select * from " + DatabaseHelper.TABLE, null);
                    }
                    else {
                        return sqlHelper.database.rawQuery("select * from " + DatabaseHelper.TABLE + " where " +
                                DatabaseHelper.COLUMN_WORD + " like ?", new String[]{"%" + constraint.toString() + "%"});
                    }
                }
            });

            inftehList.setAdapter(userAdapter);
        }
        catch (SQLException ex){}
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        sqlHelper.database.close();
        userCursor.close();
    }}

I really beliave that solution is simple, but for me its really hard to find it! Im begging you to help me. For testing I use only Samsung Galaxy A7. Hope, I wrote everything clearly.

Aucun commentaire:

Enregistrer un commentaire