Android: findViewById returns Null even if is after setContentView -
here's code in loginactivity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } loginbutton = (button)findviewbyid(r.id.loginbutton); loginbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onloginbuttonclicked(); } }); // check if there logged in user // , linked facebook account. parseuser currentuser = parseuser.getcurrentuser(); if ((currentuser != null) && parsefacebookutils.islinked(currentuser)) { // go main activity showhomeactivity(); } } in fragment_login.xml
<button android:id="@+id/loginbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="@string/login_button" /> and log when run application:
e/androidruntime(6647): fatal exception: main e/androidruntime(6647): process: com.moostachestudio.drinkitapp, pid: 6647 e/androidruntime(6647): java.lang.runtimeexception: unable start activity componentinfo{com.moostachestudio.drinkitapp/com.moostachestudio.drinkitapp.loginactivity}: java.lang.nullpointerexception e/androidruntime(6647): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) e/androidruntime(6647): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) e/androidruntime(6647): @ android.app.activitythread.access$800(activitythread.java:135) e/androidruntime(6647): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) e/androidruntime(6647): @ android.os.handler.dispatchmessage(handler.java:102) e/androidruntime(6647): @ android.os.looper.loop(looper.java:136) e/androidruntime(6647): @ android.app.activitythread.main(activitythread.java:5017) e/androidruntime(6647): @ java.lang.reflect.method.invokenative(native method) e/androidruntime(6647): @ java.lang.reflect.method.invoke(method.java:515) e/androidruntime(6647): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) e/androidruntime(6647): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) e/androidruntime(6647): @ dalvik.system.nativestart.main(native method) e/androidruntime(6647): caused by: java.lang.nullpointerexception e/androidruntime(6647): @ com.moostachestudio.drinkitapp.loginactivity.oncreate(loginactivity.java:45) e/androidruntime(6647): @ android.app.activity.performcreate(activity.java:5231) e/androidruntime(6647): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) e/androidruntime(6647): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) e/androidruntime(6647): ... 11 more the problem "loginbutton" null don't know why, because call findviewbyid after setcontentview!
it seems button in fragment layout, your on click method might in fragment (placeholderfragment) instead of activity. need declare class extends fragment..
first, create new class named placeholderfragment. extend fragment , add code below it:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_login, container, false); // ... loginbutton = (button) rootview.findviewbyid(r.id.loginbutton); loginbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onloginbuttonclicked(); } }); // ... return rootview; } // put method onloginbuttonclicked here. then, layout activity mainactivity (named activity_main) might have framelayout id container this:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <!-- other views --> </framelayout> finally, mainactivity have:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { // add fragment here in id container getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } // ... without loginbutton // , more stuff }
Comments
Post a Comment