android - NullPointer While Using a TextView to Scroll Through a ViewPager -
android - NullPointer While Using a TextView to Scroll Through a ViewPager -
i'm trying utilize textview, has card ui applied it, go through viewpager. i'm getting nullpointer when set onclicklistener.
my codefragment_a.xml
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context="com.hidden.hidden.fragmenta"> <relativelayout android:id="@+id/buttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal"> <textview android:id="@+id/next1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:padding="15dp" android:clickable="true" android:background="@drawable/selector_card_background" android:text="next" /> </relativelayout> </framelayout>
oncreate method within mainactivity.java
fragmentadapter madapter; viewpager mpager; pageindicator mindicator; textview tv1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); madapter = new fragmentadapter(getsupportfragmentmanager()); mpager = (viewpager) findviewbyid(r.id.pager); mpager.setadapter(madapter); mindicator = (linepageindicator) findviewbyid(r.id.indicator); mindicator.setviewpager(mpager); tv1 = (textview) findviewbyid(r.id.next1); //nullpointer occurs here tv1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mpager.setcurrentitem(1, true); } }); }
i tried putting onclick listener in fragment class goes along fragment_a.xml got nullpointers there because there no items.
here's nullpointer:
06-24 10:10:17.109 14227-14227/com.hidden.hidden e/androidruntime﹕ fatal exception: main process: com.hidden.hidden, pid: 14227 java.lang.runtimeexception: unable start activity componentinfo{com.hidden.hidden/com.hidden.hidden.mainactivity}: java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2184) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2233) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.hidden.hidden.mainactivity.oncreate(mainactivity.java:35) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2148) at android.app.activitythread.handlelaunchactivity(activitythread.java:2233) at android.app.activitythread.access$800(activitythread.java:135) at android.app.activitythread$h.handlemessage(activitythread.java:1196) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:136) at android.app.activitythread.main(activitythread.java:5001) at java.lang.reflect.method.invokenative(native method) at java.lang.reflect.method.invoke(method.java:515) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) at com.android.internal.os.zygoteinit.main(zygoteinit.java:601) at dalvik.system.nativestart.main(native method)
thanks in advance help!
edit:here fragmenta class:
public class fragmenta extends fragment { public fragmenta() { //required empty constructor } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { homecoming inflater.inflate(r.layout.fragment_a, container, false); } }
as said, tried making onclicklistener within fragmenta so:
tv1 = (textview) getview().findviewbyid(r.id.next1); mpager = (viewpager) getview().findviewbyid(r.id.pager); tv1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mpager.setcurrentitem(1, true); } });
but code produced this:
06-24 10:36:36.054 18426-18426/com.hidden.hidden e/androidruntime﹕ fatal exception: main process: com.hidden.hidden, pid: 18426 java.lang.nullpointerexception @ com.hidden.hidden.fragmenta$1.onclick(fragmenta.java:33) @ android.view.view.performclick(view.java:4438) @ android.view.view$performclick.run(view.java:18422) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method)
edit 2: in next giru bhai's advice, set fragmenta class so:
public class fragmenta extends fragment { viewpager mpager; textview tv1; public fragmenta() { //required empty constructor } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_a, container, false); tv1 = (textview) rootview.findviewbyid(r.id.next1); mpager = (viewpager) rootview.findviewbyid(r.id.pager); tv1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //line 38 mpager.setcurrentitem(1, true); } }); homecoming rootview; } }
now gives me nullpointer:
06-24 10:51:15.462 24389-24389/com.hidden.hidden e/androidruntime﹕ fatal exception: main process: com.hidden.hidden, pid: 24389 java.lang.nullpointerexception @ com.hidden.hidden.fragmenta$1.onclick(fragmenta.java:38) @ android.view.view.performclick(view.java:4438) @ android.view.view$performclick.run(view.java:18422) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method)
note: line 38 marked.
if referencing someing fromfragment_a.xml
null because in activity using activity_main
in oncreateview
of fragment inflate view
view v = inflater.inflate(r.layout.fragment_a, container, false);
then utilize fiew find textview
textview tv = (textview)v.findviewbyid(r.id.next1);
android android-fragments
Comments
Post a Comment