android - ViewPager Andorid:NullPointerException: Attempt to invoke virtual method -
i've got fragment have added 2 sliders in xml file:
public class filterslider extends fragment implements fragmentlifecycle { private static final string tag = filterslider.class.getsimplename(); relativelayout relativelayout; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.filter_slider, container, false); return view; }
and xml file looks:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <seekbar android:id="@+id/seekbarmin" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="20dp" android:max="100"/> <seekbar android:id="@+id/seekbarmax" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="50dp" android:max="100"/> </relativelayout>
now when trying access sliderbar main activity following codes:
here main activity class:
setcontentview(r.layout.activity_main); pageadapter = new mypageradapter(getsupportfragmentmanager()); final viewpager pager = (viewpager)findviewbyid(r.id.myviewpager); pager.setadapter(pageadapter); seekbarmin = (seekbar) findviewbyid(r.id.seekbarmin); seekbarmax = (seekbar) findviewbyid(r.id.seekbarmax); slidermaxtemp = seekbarmax.getprogress();
adn here mypageadapter class:
private list<fragment> fragments; public mypageradapter(fragmentmanager fm) { super(fm); this.fragments = new arraylist<fragment>(); fragments.add(new filtercheckbox()); fragments.add(new filterradiobutton()); fragments.add(new filterslider()); }
it shows following error:
java.lang.nullpointerexception: attempt invoke virtual method 'int android.widget.seekbar.getprogress()' on null object reference.
i highly appreciate suggestions solve error.
i've tested on real device (api10) , works. (rewritten many parts.. use diff source check them.)
need more information find out problem..
// part of manifest <uses-sdk android:minsdkversion="10" />
//mainactivity.java // changed imports. // must check v4 library class or not. (for each ui parts class etc.) import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import android.view.view; import android.view.view.onclicklistener; // selectbutton_* click events. import android.widget.seekbar; // changed activity -> fragmentactivity use getsupportfragmentmanager() public class mainactivity extends fragmentactivity { private viewpager pager; private seekbar seekbarmin; private seekbar seekbarmax; private int slidermaxtemp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); init(); } private void init() { mypageradapter pageadapter = new mypageradapter(getsupportfragmentmanager()); pager = (viewpager) findviewbyid(r.id.myviewpager); pager.setadapter(pageadapter); seekbarmin = (seekbar) findviewbyid(r.id.seekbarmin); seekbarmax = (seekbar) findviewbyid(r.id.seekbarmax); slidermaxtemp = seekbarmax.getprogress(); // events choose page. // may want use ontouch or gesture.. final int idx_checkbox = 0; final int idx_radiobutton = 1; final int idx_slider = 2; findviewbyid(r.id.selectbutton_checkbox).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // select page pager.setcurrentitem(idx_checkbox); } }); findviewbyid(r.id.selectbutton_radiobutton).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { pager.setcurrentitem(idx_radiobutton); } }); findviewbyid(r.id.selectbutton_slider).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { pager.setcurrentitem(idx_slider); } }); } }
// filterslider.java import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.relativelayout; // changed "v4" fragment public class filterslider extends fragment { private static final string tag = filterslider.class.getsimplename(); relativelayout relativelayout; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.filter_slider, container, false); return view; } }
// mypageradapter.java import java.util.arraylist; import java.util.list; // changed "v4" classes import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; // changed pageradapter -> fragmentstatepageradapter public class mypageradapter extends fragmentstatepageradapter { private list<fragment> fragments; mypageradapter(fragmentmanager fm) { super(fm); this.fragments = new arraylist<fragment>(); fragments.add(new filtercheckbox()); fragments.add(new filterradiobutton()); fragments.add(new filterslider()); } @override public int getcount() { return fragments.size(); } @override public fragment getitem(int position) { // returns fragment want show try { return fragments.get(position); } catch (indexoutofboundsexception e) { // todo: handle exception return null; } } }
activity_main.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" > <seekbar android:id="@+id/seekbarmin" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="20dp" android:max="100" /> <seekbar android:id="@+id/seekbarmax" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="50dp" android:max="100" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" > <button android:id="@+id/selectbutton_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="checkbox" /> <button android:id="@+id/selectbutton_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="radiobutton" /> <button android:id="@+id/selectbutton_slider" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="slider" /> </linearlayout> <android.support.v4.view.viewpager android:id="@+id/myviewpager" android:layout_width="250dp" android:layout_height="wrap_content" /> </linearlayout>
filter_slider.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" > <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="filter slider" android:textappearance="?android:attr/textappearancemedium" /> </linearlayout>
Comments
Post a Comment