android - viewpager = (ViewPager)findViewById(R.id.pager); returns NULL, how to use .addNext() and .showNext() -
i'm trying instantiate viewpager in app can't find way make viewpager object non-null. everytime call viewpager = (viewpager)findviewbyid(r.id.pager); oncreate() method in main activity, viewpager object returned null. why this?
i'm doing view manipulation in asynctask thread launches before , displays splash screen in addition loading files. in thread manipulate view using viewswitcher object , built in methods such .addview().
loadnotefiles.java
protected void onpreexecute(){ playfrets.viewswitch = new viewswitcher(mcontext); playfrets.viewswitch.addview(viewswitcher.inflate(mcontext, r.layout.splash_screen, null)); instance = (activity)mcontext; //cast context main activity activity access setcontentview method instance.setcontentview(playfrets.viewswitch); } ... @override protected void onpostexecute(void v){ playfrets.viewswitch.addview(viewswitcher.inflate(mcontext, r.layout.activity_play_frets, null)); playfrets.viewswitch.shownext(); ((playfrets)mcontext).configfretboard(); } should trying add each menu fragment view viewswitcher object using .addview()? causing viewpager null? i'm not clear on when , how need use viewswitcher object , methods .addview() , .shownext(), background explanation wonderful.below xml files , relevant code main activity.
playfrets.java
@override protected void oncreate(bundle savedinstancestate) { setrequestedorientation(activityinfo.screen_orientation_portrait); //sets activity orientation portrait //build gui super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); //removes action bar display getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); //removes status bar display //create asynctask load note files. splash screen displayed while task executing new loadnotefiles(this).execute(); //set menu swipe capability //implements viewpager class viewpager = (viewpager)findviewbyid(r.id.pager); //viewpager null here pageradapter pageadapter = new menupageradapter(getsupportfragmentmanager()); viewpager.setadapter(pageadapter); //causes nullpointer exception here } activity_play_frets.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselinealigned="false" android:keepscreenon="true"> <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/gold_guitarsim_text" android:orientation="vertical" android:id="@+id/toolbargestureoverlay" > <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.viewpager> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="4"> <com.example.guitarsim.multitouch android:id="@+id/fretboard" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/fret_board1" /> </linearlayout> </linearlayout> sample menu fragment 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"> </linearlayout>
call
setcontentview(r.layout.activity_play_frets) after getwindow() function
Comments
Post a Comment