0

I'm beginner and I'm trying to developing simple android app, but I would need some help. I have main activity with some buttons, labels and so on. After I press one button, I launch second activity, SearchActivity and here is where I need help.

SearchActivity.java

public class SearchActivity extends AppCompatActivity {  

      ListView lv;
        SearchView sv;
         ArrayAdapter<String> adapter;
        private ArrayList<String> items = getResults();

        private ArrayList<String> getResults(){
            DatabaseHelper db = new DatabaseHelper(this);
            db.openDatabase();
            ArrayList<String> resultList = new ArrayList<String>();
            Cursor c = db.query(tablename,column names,null,null,null,null,null);
            c.close();
            db.close();
            return  resultList;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search);
            lv = (ListView)findViewById(R.id.listview);
            sv = (SearchView)findViewById(R.id.search);
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items);
            lv.setAdapter(adapter);
            sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    adapter.getFilter().filter(newText);
                    return false;
                }
            });


        }
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper
{

     String DB_PATH = null;
        private static String DB_NAME = "dat";
        private SQLiteDatabase myDatabase;
        private final Context myContext;

        public DatabaseHelper(Context context){
            super(context, DB_NAME, null, 10);
            this.myContext = context;
            this.DB_PATH = "/data/data" + context.getPackageName() + "/" + "databases/";
            Log.e("Path 1", DB_PATH);
        }

        public void createDatabase() throws  IOException
        {
            boolean dbExist = checkDatabase();
            if (dbExist) {
            } else {
                this.getReadableDatabase();
                try {
                   copyDatabse();

                } catch (IOException e){
                    throw new Error("Error copying database");
                }
            }

        }

        private boolean checkDatabase()
        {
            SQLiteDatabase checkDB = null;
            try {
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
                } catch (SQLiteException e){
             }
             if(checkDB != null){
                 checkDB.close();
             }
             return checkDB != null ? true : false;
        }

        private  void copyDatabse() throws IOException
        {
            InputStream myInput = myContext.getAssets().open(DB_NAME);
            String outFileName = DB_PATH + DB_NAME;
            OutputStream myOutput =  new FileOutputStream(outFileName);
            byte[] buffer = new byte[10];
            int length;
            while((length = myInput.read(buffer)) > 0){
                myOutput.write(buffer, 0, length);
            }
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }

        public void openDatabase() throws SQLiteException
        {
            String myPath = DB_PATH + DB_NAME;
            myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        }

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

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if(newVersion > oldVersion){
                try{
                    copyDatabse();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }

        public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
        {
            return myDatabase.query(table,columns,null,null,null,null,null);

        }
}

And activity_search.xml contains only one SearchView and one ListView.

What I'm trying here is to load table data from database and be able to search from them by using SearchView and show results in TextView, with only string array it works, but with this, the whole SearchActivity cause that app crashes.

Here are my errors:

Caused by: android.database.sqlite.SQLiteException: no such table: Zastavky (code 1): , while compiling: SELECT Nazev FROM Zastavky
                                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                     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:1316)
                                                                     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                                                                     at com.example.geote.ap1.SearchActivity.fetchData(SearchActivity.java:52)
                                                                     at com.example.geote.ap1.SearchActivity.onCreate(SearchActivity.java:29)
                                                                     at android.app.Activity.performCreate(Activity.java:5990)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                     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:903) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

 

Thank you for response.

dom smrc
  • 1
  • 2
  • Where are your classes? – Code-Apprentice Apr 15 '17 at 18:49
  • "the whole SearchActivity cause that app fails" Please explain exactly what happens when the "app fails". – Code-Apprentice Apr 15 '17 at 18:50
  • When I click on button to launch SearchActivity, I got message "App has stopped". So, the whole app fails. Where are my classes? Well, java are in app>java and xml, it's a layout so in app>layout. – dom smrc Apr 15 '17 at 21:40
  • [Here are some tips to help you start to figure out why your app crashes](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). When I asked where your class are, I meant that none of tree code you posted is in a class, so it gives me compiler errors. Please edit your code to include classes. – Code-Apprentice Apr 16 '17 at 00:14
  • So, I edited with classes. I inspected the code and apparently it can't load databse from assets, but I have no idea why. I added error which I'm getting. – dom smrc Apr 16 '17 at 11:07
  • Please post the rest of the stacktrace. You need to go down to where it says "caused by NullPointerException" to find which line causes the error. – Code-Apprentice Apr 16 '17 at 14:46
  • Posted full stack trace, probably caued by either empty DB or that app doesn't copy DB to /data/data/package-name/databases. I don't really know, but I was looking on Device Monitor and file explorer and DB is not there, so this is probably the problem. But I don't know why, 'cause I have it in assets. – dom smrc Apr 16 '17 at 19:48
  • at com.example.geote.ap1.SearchActivity.fetchData(SearchActivity.java:52) This says the crash occurs on line 52 of SearchActivity.java in the fetchData() method. What lined is that? – Code-Apprentice Apr 16 '17 at 20:46
  • Note that you should never assume that the directory structure on your development machine is recreate on the target environment. This applies to all programming projects and especially to Android. There is no assets folder on the device. – Code-Apprentice Apr 16 '17 at 20:49
  • Possible duplicate of [Import Existing SQLite Database into Android Application](http://stackoverflow.com/questions/33668046/import-existing-sqlite-database-into-android-application) – Code-Apprentice Apr 16 '17 at 20:53
  • There are many questions already on Stack Overflow about reading a database from the assets folder of your project. I have linked one above. You should google for more. – Code-Apprentice Apr 16 '17 at 20:54

0 Answers0