android - Eclipse: error java.lang.NullPointerException -
this happens when input information on popupwindow
.
java code:
package com.example.istronggyminstructor; import android.os.bundle; import android.app.activity; import android.content.intent; import android.database.cursor; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.view.viewgroup.layoutparams; import android.view.windowmanager; import android.widget.button; import android.widget.edittext; import android.widget.framelayout; import android.widget.popupwindow; import android.widget.textview; import android.widget.toast; public class currentusers extends activity { private button register; private button adduser; edittext getusertext, getpass, getweight, textdisp; dbadapter mydb; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_current_users); register = (button)findviewbyid(r.id.regbut); adduser = (button)findviewbyid(r.id.addbut); register.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { layoutinflater inflator=(layoutinflater)getbasecontext().getsystemservice(layout_inflater_service); view popupview=inflator.inflate(r.layout.popup, null); final popupwindow popupwindow = new popupwindow(popupview, layoutparams.wrap_content,layoutparams.wrap_content); popupwindow.showatlocation(popupview, gravity.center, 0, 0); popupwindow.setfocusable(true); popupwindow.update(); button dismissbtn = (button)popupview.findviewbyid(r.id.close); dismissbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { popupwindow.dismiss(); } }); popupwindow.showasdropdown(register, 50, -30); } }); opendb(); } public void opendb(){ mydb = new dbadapter(this); mydb.open(); } private void closedb() { mydb.close(); } private void displaytext(string message) { textview textview = (textview) findviewbyid(r.id.textdisp); textview.settext(message); } public void registeruser(view v) { edittext username = (edittext)findviewbyid(r.id.usertext); edittext password = (edittext)findviewbyid(r.id.passwordtext); edittext weight = (edittext)findviewbyid(r.id.weight); string getusername = username.gettext().tostring(); string getpassword = password.gettext().tostring(); int getweight = integer.parseint(weight.gettext().tostring()); long newid = mydb.insertrow(getusername, getpassword, getweight); cursor cursor = mydb.getrow(newid); displayrecordset(cursor); } public void onclick_clearall(view v) { mydb.deleteall(); } public void onclick_displayrecords(view v) { cursor cursor = mydb.getallrows(); displayrecordset(cursor); } private void displayrecordset(cursor cursor) { string message = ""; if (cursor.movetofirst()) { { int id = cursor.getint(dbadapter.col_rowid); string name = cursor.getstring(dbadapter.col_name); string password = cursor.getstring(dbadapter.col_password); int weight = integer.parseint(cursor.getstring(dbadapter.col_weight)); message += "id=" + id +", name=" + name +", #=" + password +", colour=" + weight +"\n"; } while(cursor.movetonext()); } cursor.close(); displaytext(message); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.current_users, menu); return true; } }
xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <framelayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.16" > <textview android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingleft="220dp" android:text="@string/title_activity_current_users" android:textsize="25sp" android:textstyle="bold" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="100dp" android:text="@string/name" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="200dp" android:paddingtop="100dp" android:text="@string/timein" /> <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="400dp" android:paddingtop="100dp" android:text="@string/score" /> <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingleft="540dp" android:paddingtop="100dp" android:text="@string/done" /> <button android:id="@+id/regbut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/register" /> <button android:id="@+id/addbut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="505dp" android:onclick="onclick_clearall" android:text="@string/adduser" /> </framelayout> <scrollview android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <textview android:id="@+id/textdisp" android:layout_width="match_parent" android:layout_height="2000dp" /> </linearlayout> </scrollview> </linearlayout>
popupwindow xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/darker_gray" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/register" android:textsize="20sp" /> <textview android:text="@string/desireduser" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/usertext" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="text" /> <textview android:text="@string/desiredpass" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/passwordtext" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputtype="textpassword" /> <textview android:text="@string/weightinpounds" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/weight" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputtype="number" /> <button android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="registeruser" android:text="@string/register2" /> <button android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/close" /> </linearlayout>
i tried not using edittexts still same. please me thanks!
edit logcat (sorry)
08-18 14:42:43.062: e/androidruntime(9748): fatal exception: main 08-18 14:42:43.062: e/androidruntime(9748): java.lang.illegalstateexception: not execute method of activity 08-18 14:42:43.062: e/androidruntime(9748): @ android.view.view$1.onclick(view.java:3599) 08-18 14:42:43.062: e/androidruntime(9748): @ android.view.view.performclick(view.java:4204) 08-18 14:42:43.062: e/androidruntime(9748): @ android.view.view$performclick.run(view.java:17355) 08-18 14:42:43.062: e/androidruntime(9748): @ android.os.handler.handlecallback(handler.java:725) 08-18 14:42:43.062: e/androidruntime(9748): @ android.os.handler.dispatchmessage(handler.java:92) 08-18 14:42:43.062: e/androidruntime(9748): @ android.os.looper.loop(looper.java:137) 08-18 14:42:43.062: e/androidruntime(9748): @ android.app.activitythread.main(activitythread.java:5041) 08-18 14:42:43.062: e/androidruntime(9748): @ java.lang.reflect.method.invokenative(native method) 08-18 14:42:43.062: e/androidruntime(9748): @ java.lang.reflect.method.invoke(method.java:511) 08-18 14:42:43.062: e/androidruntime(9748): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-18 14:42:43.062: e/androidruntime(9748): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-18 14:42:43.062: e/androidruntime(9748): @ dalvik.system.nativestart.main(native method) 08-18 14:42:43.062: e/androidruntime(9748): caused by: java.lang.reflect.invocationtargetexception 08-18 14:42:43.062: e/androidruntime(9748): @ java.lang.reflect.method.invokenative(native method) 08-18 14:42:43.062: e/androidruntime(9748): @ java.lang.reflect.method.invoke(method.java:511) 08-18 14:42:43.062: e/androidruntime(9748): @ android.view.view$1.onclick(view.java:3594) 08-18 14:42:43.062: e/androidruntime(9748): ... 11 more 08-18 14:42:43.062: e/androidruntime(9748): caused by: java.lang.nullpointerexception 08-18 14:42:43.062: e/androidruntime(9748): @ com.example.istronggyminstructor.currentusers.registeruser(currentusers.java:75) 08-18 14:42:43.062: e/androidruntime(9748): ... 14 more
possibly mydb
null (as have not given complete stack trace).
before doing database operations, open database. need call method opendb()
before doing db related operations.
without initializing mydb
doing db operations i.e. mydb.insertrow(getusername, getpassword, getweight);
, cursor cursor = mydb.getrow(newid);
. here mydb
null because have not initialized i.e. mydb = new dbadapter(this)
edit
you have 2 options. 1) make view popupview
variable global. replace
view popupview=inflator.inflate(r.layout.popup, null);
by
popupview=inflator.inflate(r.layout.popup, null);
afterwards inside method registeruser(view v)
, replace
edittext username = (edittext)findviewbyid(r.id.usertext); edittext password = (edittext)findviewbyid(r.id.passwordtext); edittext weight = (edittext)findviewbyid(r.id.weight);
by
edittext username = (edittext)popupview.findviewbyid(r.id.usertext); edittext password = (edittext)popupview.findviewbyid(r.id.passwordtext); edittext weight = (edittext)popupview.findviewbyid(r.id.weight);
2) move
edittext username = (edittext)findviewbyid(r.id.usertext); edittext password = (edittext)findviewbyid(r.id.passwordtext); edittext weight = (edittext)findviewbyid(r.id.weight);
from registeruser(view v)
, paste them after
button dismissbtn = (button)popupview.findviewbyid(r.id.close);
so should this
button dismissbtn = (button)popupview.findviewbyid(r.id.close); edittext username = (edittext)popupview.findviewbyid(r.id.usertext); edittext password = (edittext)popupview.findviewbyid(r.id.passwordtext); edittext weight = (edittext)popupview.findviewbyid(r.id.weight);
Comments
Post a Comment