Why can't my Android app see the database file? -
i'm creating android app, , want make registration. have written file c.r.u.d database. when start app , try add somebody, can see popup message:
"unfortunately , application has been stopped".
here database controller code:
package com.example.lingwista.lingwista; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class db_controller extends sqliteopenhelper { public db_controller(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, "lingwista.db", factory, version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table users( id integer primary key autoincrement, username text unique, password text);"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists users;"); } public void insert_user(string username, string password){ contentvalues contentvalues = new contentvalues(); contentvalues.put("username",username); contentvalues.put("password",password); this.getwritabledatabase().insertorthrow("users", "", contentvalues); } public boolean search_user(string username){ cursor cursor = this.getreadabledatabase().rawquery("select username users",null); while (cursor.movetonext()){ if(cursor.getstring(1).equals(username)){ return true; } } return false; } public boolean search_password(string password){ cursor cursor = this.getreadabledatabase().rawquery("select password users",null); while (cursor.movetonext()){ if(cursor.getstring(1).equals(password)){ return true; } } return false; }
}
this java file use controller:
package com.example.lingwista.lingwista; import android.content.context; import android.content.intent; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.edittext; import android.widget.toast; public class rejestracja extends appcompatactivity { db_controller controller; edittext username; edittext password1; edittext password2; string login; string haslo1; string haslo2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_rejestracja); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); username = (edittext)findviewbyid(r.id.edittext3); password1 = (edittext)findviewbyid(r.id.edittext4); password2 = (edittext)findviewbyid(r.id.edittext5); controller = new db_controller(this, "", null, 1); } public void gotomenu(view view) { login = username.gettext().tostring(); haslo1 = password1.gettext().tostring(); haslo2 = password2.gettext().tostring(); boolean u; string message; context context = getapplicationcontext(); int duration = toast.length_short; if(haslo1.equals(haslo2)){ u = controller.search_user(login); if(u==true){ message = "istnieje już użytkownik który korzysta z tego adresu email"; toast toast = toast.maketext(context,message, duration); toast.show(); } else{ controller.insert_user(login,haslo1); intent intent = new intent(".menu"); startactivity(intent); } }else{ message = "błąd hasła. proszę upewnić się ze oba pola zawierają samo hasło"; toast toast = toast.maketext(context,message, duration); toast.show(); } if(login.equals("") || haslo1.equals("") || haslo2.equals("")){ message = "wszystkie pola muszą być wypełnione !"; toast toast = toast.maketext(context,message, duration); toast.show(); } } @override public void onpause(){ super.onpause(); finish(); } public void back(view view) { intent intent =new intent(".mainmenuactivity"); startactivity(intent); } public void exitapp(view view) { system.exit(0); } }
and content.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.lingwista.lingwista.rejestracja" tools:showin="@layout/activity_rejestracja"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview2" android:layout_alignparenttop="true" android:src="@drawable/lingwista" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="rejestracja" android:id="@+id/textview3" android:layout_below="@+id/imageview2" android:layout_centerhorizontal="true" android:textcolor="#00ff00" android:layout_margintop="30dp" android:textsize="38dp" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textemailaddress" android:ems="10" android:id="@+id/edittext3" android:layout_below="@+id/textview3" android:layout_margintop="10dp" android:layout_centerhorizontal="true" android:autotext="false" android:hint="adres email" android:backgroundtint="#0000ff" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpassword" android:ems="10" android:id="@+id/edittext4" android:layout_below="@+id/edittext3" android:layout_margintop="5dp" android:layout_centerhorizontal="true" android:hint="hasło" android:backgroundtint="#0000ff" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpassword" android:ems="10" android:id="@+id/edittext5" android:layout_below="@+id/edittext4" android:layout_alignright="@+id/edittext4" android:layout_margintop="5dp" android:layout_alignend="@+id/edittext4" android:hint="powtorz hasło" android:backgroundtint="#0000ff" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="zarejestruj" android:id="@+id/button12" android:layout_margintop="52dp" android:layout_below="@+id/edittext5" android:layout_centerhorizontal="true" android:textcolor="#ffffff" android:textsize="20dp" android:background="#0000ff" android:onclick="gotomenu"/> </relativelayout>
this error log:
01-16 02:55:10.539 18767-18791/com.example.lingwista.lingwista e/surface: getslotfrombufferlocked: unknown buffer: 0xab855e50 01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista e/cursorwindow: failed read row 0, column 1 cursorwindow has 1 rows, 1 columns. 01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista d/androidruntime: shutting down vm 01-16 02:55:13.104 18767-18767/com.example.lingwista.lingwista e/androidruntime: fatal exception: main process: com.example.lingwista.lingwista, pid: 18767 java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:275) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:270) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.illegalstateexception: couldn't read row 0, col 1 cursorwindow. make sure cursor initialized correctly before accessing data it. @ android.database.cursorwindow.nativegetstring(native method) @ android.database.cursorwindow.getstring(cursorwindow.java:438) @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:51) @ com.example.lingwista.lingwista.db_controller.search_user(db_controller.java:35) @ com.example.lingwista.lingwista.rejestracja.gotomenu(rejestracja.java:47) @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:270) @ android.view.view.performclick(view.java:5198) @ android.view.view$performclick.run(view.java:21147) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 01-16 02:55:19.507 18767-18767/? i/process: sending signal. pid: 18767 sig: 9
the error clear:
caused by: java.lang.illegalstateexception: couldn't read row 0, col 1 cursorwindow. make sure cursor initialized correctly before accessing data it. @ android.database.cursorwindow.nativegetstring(native method) @ android.database.cursorwindow.getstring(cursorwindow.java:438) @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:51) @ com.example.lingwista.lingwista.db_controller.search_user(db_controller.java:35)
here's offending code that's crashing:
public boolean search_user(string username){ cursor cursor = this.getreadabledatabase().rawquery("select username users",null); while (cursor.movetonext()){ if(cursor.getstring(1).equals(username)){ return true; } } return false; }
the columns have zero-based index , asking 1 column in query. such should cursor.getstring(0)
cursor.getstring(int columnindex) documentation:
returns value of requested column string.
the result , whether method throws exception when column value null or column type not string type implementation-defined.
parameters
columnindex - zero-based index of target column.
returns
the value of column string.
also note written null reference if use correct column. want like:
if (!cursor.isnull(0) && cursor.getstring(0).equals(username)) {
Comments
Post a Comment