android - NPE on clicking button in fragments and app crashes after that -
i calling fragment 1 of activities , has login button in fragment itself. giving npe, moment click button.i have tried permutation , combination make work. here's code
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreate(savedinstancestate); setuservisiblehint(true); view rootview = inflater.inflate(r.layout.fragment1, container, false); button button = (button) rootview.findviewbyid(r.id.bt_login); button.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { // perform action on click edittext username = (edittext)v.findviewbyid(r.id.username); string user_name = username.gettext().tostring(); edittext password = (edittext)v.findviewbyid(r.id.password); string user_password = password.gettext().tostring(); if(user_name.length()== 0 || user_password.length()== 0) { toast.maketext(getactivity(), "please enter correct information", toast.length_long).show(); //toast.maketext(this,"please enter correct information",toast.length_long).show(); return; } loginaction(user_name,user_password); } }); return rootview; }
stacktrace
07-11 11:50:02.536: e/androidruntime(429): fatal exception: main 07-11 11:50:02.536: e/androidruntime(429): java.lang.nullpointerexception 07-11 11:50:02.536: e/androidruntime(429): @ com.androidbegin.sidemenututorial.fragment1$1.onclick(fragment1.java:46) 07-11 11:50:02.536: e/androidruntime(429): @ android.view.view.performclick(view.java:2408) 07-11 11:50:02.536: e/androidruntime(429): @ android.view.view$performclick.run(view.java:8816) 07-11 11:50:02.536: e/androidruntime(429): @ android.os.handler.handlecallback(handler.java:587) 07-11 11:50:02.536: e/androidruntime(429): @ android.os.handler.dispatchmessage(handler.java:92) 07-11 11:50:02.536: e/androidruntime(429): @ android.os.looper.loop(looper.java:123) 07-11 11:50:02.536: e/androidruntime(429): @ android.app.activitythread.main(activitythread.java:4627) 07-11 11:50:02.536: e/androidruntime(429): @ java.lang.reflect.method.invokenative(native method) 07-11 11:50:02.536: e/androidruntime(429): @ java.lang.reflect.method.invoke(method.java:521) 07-11 11:50:02.536: e/androidruntime(429): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 07-11 11:50:02.536: e/androidruntime(429): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 07-11 11:50:02.536: e/androidruntime(429): @ dalvik.system.nativestart.main(native method)
you should use rootview inflate views.
edittext username = (edittext)rootview.findviewbyid(r.id.username);
Comments
Post a Comment