ViewPager and dynamic fragments : re-instaciation gives NPE - android

I faced an issue with ViewPager and ViewPagerAdapter in Android.
I use a viewpager with 2 static fragments (one using a textEdit and the second one using a listview). Theyr are working pretty good.
But i have a problem with the third fragment which is dynamic.
It uses the camera and has to be instanciated, destroyed, re-instanciated following a scenario. So, the ViewPagerAdapter could contain 2 or 3 framents.
The problem appears when I re-instaciate the third fragment, I got a NPE after OnCreateView() (the main layout view is null after this method, but is not null inside the method).
There is the code for the main activity :
mViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager();
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(mViewPager);
The setupViewPager() :
private void setupViewPager() {
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mTabFragmentDocument = new TabFragmentDocument();
mTabFragmentDocument.setApp(this);
mTabFragmentText = new TabFragmentText();
mTabFragmentText.setApp(this);
mViewPagerAdapter.addFragment(mTabFragmentText, AbstractDefiner.TEXT);
mViewPagerAdapter.addFragment(mTabFragmentDocument, AbstractDefiner.DOCUMENT);
mViewPager.setAdapter(mViewPagerAdapter);
}
To create the third fragment :
mTabFragment = new TabFragment();
mTabFragment .setApp(this);
mViewPagerAdapter.addFragment(mTabFragment, "THIRD");
mViewPagerAdapter.notifyDataSetChanged();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(2);
mViewPagerAdapter.notifyDataSetChanged();
mTabFragment .setParams(tmp[1], tmp[2], tmp[3], tmp[4]);
mTabFragment .setupView();
mTabFragment .startWork();
}
}, 1000);
And to destroy it :
mViewPager.setCurrentItem(0);
mViewPager.removeViewAt(2);
mTabFragment .onDestroy();
mViewPagerAdapter.remove(2);
mViewPagerAdapter.notifyDataSetChanged();
mTabFragment = null;
Then, the Adapter code :
static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public void remove(int index) {
mFragmentList.remove(index);
mFragmentTitleList.remove(index);
}
#Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Finally, the code of the third fragment :
public class TabFragment extends Fragment implements SurfaceHolder.Callback,
GLSurfaceView.Renderer {
private static final String LOGCAT = "WEB_RTC_VISIO";
private HomeActivity mApp;
private String p1;
private String p2;
private String p3;
private String p4;
private VideoSource mLocalVideoSource;
private VideoRenderer.Callbacks mLocalRenderer;
private VideoRenderer.Callbacks mRemoteRenderer;
private GLSurfaceView mVideoView;
private SurfaceView mDrawView;
private SurfaceHolder mDrawHolder;
private ImageView mCursor;
private String mBgBytesString;
private ImageView mImgView;
private View mV;
public TabFragment() {
// Required empty public constructor
}
public void setApp(HomeActivity app) {
mApp = app;
}
public void setParams(String p1, String p2, String p3,
String p4) {
this.p1= p1;
this.p2= p2;
this.p4= p4;
this.p3= p3;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mV = inflater.inflate(R.layout.third_layout, container, false);
return mV;
}
#Override
public void onDetach() {
super.onDetach();
}
public void setupView() {
RequestUserPermission requestUserPermission = new RequestUserPermission(mApp);
requestUserPermission.verifyStoragePermissions();
mImgView = (ImageView) mV.findViewById(R.id.img_display); // NPE HERE
TextView mTest1= (TextView) mV.findViewById(R.id.test1);
mRequestLabelTextView.setText("test 1");
TextView mTest2= (TextView) mV.findViewById(R.id.test2);
mEquipmentSerialTextView.setText("test 2");
// View that displays the view from the camera
mVideoView = (GLSurfaceView) mV.findViewById(R.id.gl_surface);
// View that displays the cursor and drawing associated
mDrawView = (SurfaceView) mV.findViewById(R.id.draw_surface);
mDrawHolder = mDrawView.getHolder();
mDrawHolder.setFormat(PixelFormat.TRANSPARENT);
mDrawHolder.addCallback(this);
// Image of the cursor
mCursor = (ImageView) mV.findViewById(R.id.mouseCursor);
// Some more inits
}
public void startWork() {
//SOME WORK
}
}
So, the first instanciation is ok, but at the second, I got the NPE on getting the ImageView...
Someone can help me understanding this problem please ?
Thanks in advance !

Try using FragmentStatePagerAdapter not FragmentPagerAdapter it will solve many problems. Hope it will gonna work for you too.

Related

Fragment not visible in ViewPager

I have a ViewPager that I want to add Fragments to.
The ViewPager is viewed correctly on the screen but unfortunately it does not hold any content.
I have seen the suggestion in some posts to replace
getSupportFragmentManager()
with
getChildFragmentManager()
But I need the ActionBar. Unfortunately the ChildFragmentmanager is not available in the AppCompatActivity.
While debugging I have realized that the Fragment's onCreateView() method is called and it returns a layout.
Question: How can I make the Fragments visible? And were is the main difference from my code to that sample ?
Activity:
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager mViewPager;
private List<Location> locationList = new ArrayList<>();
private locationsBaseHandler db;
private CustomPagerAdapter mAdapter;
private Location mLocation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
locationList.add(someData);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mAdapter = new CustomPagerAdapter(this, getSupportFragmentManager(), locationList);
mViewPager.setAdapter(mAdapter);
}
}
Pager Adapter:
public class CustomPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
private List<Location> locationList = new ArrayList<>();
public CustomPagerAdapter(Context mContext, FragmentManager fm, List<Location> locationList) {
super(fm);
this.locationList.addAll(locationList);
this.mContext = mContext;
}
#Override
public Fragment getItem(int position) {
return LocationFragment.newInstance(position);
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
}
Fragment:
public class LocationFragment extends Fragment {
private static final String ARG_LAYOUT_ID = "page_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_location, container, false);
return layout;
}
public static LocationFragment newInstance(int selectedIdForIndex) {
LocationFragment fragment = new LocationFragment();
Bundle args = new Bundle();
args.putInt(ARG_LAYOUT_ID, selectedIdForIndex);
fragment.setArguments(args);
return fragment;
}
public int getPageNumber() {
return getArguments().getInt(ARG_LAYOUT_ID);
}
}
Try to remove the method isViewFromObject in CustomPagerAdapter.
#Override
public boolean isViewFromObject(View view, Object object) {
return false;
}
We can see this comment to isViewFromObject:
Determines whether a page View is associated with a specific key object
as returned by {#link #instantiateItem(ViewGroup, int)}. This method is
required for a PagerAdapter to function properly.

Fragment in ViewPager is not displaying anything in its RecyclerView on rotation

I have 4 Fragments in a ViewPager that is integrated with a TabLayout. Each of those Fragments holds a RecyclerView because I'm displaying an unknown amount of list items. The items are loaded by date, so I have two buttons that let you change days, and then the appropriate data in the list items is loaded based on the date that is set. The buttons are part of the outer activity.
Everything so far works perfectly BUT once I rotate the device the fragments don't load anything. Nothing is displayed inside the Fragments. The views in the outer activity are displayed fine though. The weird thing is that if I touch one of the buttons that changes the date, the Fragments are loaded and the data is displayed correctly. But I need everything to be displayed immediately after rotation not just when I click one of those buttons. Here is my code:
public class MainActivity extends AppCompatActivity implements DatePickerFragment.DateDialogInterface
{
private final static String COLOR_CONTROL_CHOSEN = "color_control_chosen";
private static final String DIALOG_DATE = "dialog_date";
private static final String TAG = "MainActivity";
private static final String SET_LOCATION = "set_location";
public static final String LEAGUE_POSITION = "league_position";
private Toolbar mToolbar;
private ViewPager mViewPager;
private TabLayout mTabLayout;
private ImageButton mPrevDateButton, mNextDateButton;
private TextView mDateTextView, mLeagueTextView;
private String mTodaysDate;
private Date mLastSelectedDate, mTodaysDateObj;
private ProgressBar mProgressBar;
private FragmentPagerAdapter mAdapterViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.outnix_toolbar);
mViewPager = (ViewPager) findViewById(R.id.main_activity_view_pager);
mTabLayout = (TabLayout) findViewById(R.id.main_activity_tab_layout);
mDateTextView = (TextView) findViewById(R.id.display_date_text_view);
mLeagueTextView = (TextView) findViewById(R.id.league_title_header);
mPrevDateButton = (ImageButton) findViewById(R.id.previous_date_button);
mNextDateButton = (ImageButton) findViewById(R.id.next_date_button);
mProgressBar = (ProgressBar) findViewById(R.id.loading_circle);
mProgressBar.setVisibility(View.GONE);
setTodaysDate();
mDateTextView.setText("Today");
setSupportActionBar(mToolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowTitleEnabled(false);
NavDrawerFragment navDrawerFrag = (NavDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_nav_drawer);
navDrawerFrag.setUp((DrawerLayout) findViewById(R.id.nav_drawer), mToolbar);
setUpViewPagerAdapterOnCreate();
mTabLayout.setupWithViewPager(mViewPager);
mDateTextView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentManager fm = getSupportFragmentManager();
DatePickerFragment dialog = new DatePickerFragment();
Bundle args = new Bundle();
args.putSerializable("ARG_DATE", mLastSelectedDate);
dialog.setArguments(args);
dialog.show(fm, DIALOG_DATE);
}
});
THESE TWO LISTENERS ARE WHAT DISPLAY THE DATA AFTER A ROTATION CHANGE
//////////////////////////////////////////////////////////////////
mPrevDateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
mProgressBar.setVisibility(View.VISIBLE);
updateDate(-1);
setupViewPagerAdapter(getViewPagerPos());
}
});
mNextDateButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
mProgressBar.setVisibility(View.VISIBLE);
updateDate(1);
setupViewPagerAdapter(getViewPagerPos());
}
});
}
////////////////////////////////////////////////////////////////////
/* #Override
protected void onResume()
{
super.onResume();
setupViewPagerAdapter(getViewPagerPos());
}*/
private void setUpViewPagerAdapterOnCreate() //only called when opened from LauncherActivity
{
setupViewPagerAdapter(0);
int touchColorPixel = getIntent().getIntExtra(COLOR_CONTROL_CHOSEN, ContextCompat.getColor(this, R.color.blue));
if(touchColorPixel == ContextCompat.getColor(this, R.color.orange))
mViewPager.setCurrentItem(0);
else if(touchColorPixel == ContextCompat.getColor(this, R.color.blue))
mViewPager.setCurrentItem(1);
else
mViewPager.setCurrentItem(3);
}
private void setupViewPagerAdapter(int currentPos)
{
mAdapterViewPager = new MainPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapterViewPager);
mViewPager.setOffscreenPageLimit(4);
mViewPager.setCurrentItem(currentPos);
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putSerializable(DIALOG_DATE, mLastSelectedDate); //save the last date selected (or last date displayed) on rotation change.
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
setDateFromDialog((Date) savedInstanceState.getSerializable(DIALOG_DATE)); //restore the last date selected to the new rotation change
}
public ProgressBar getProgressBar()
{
return mProgressBar;
}
#Override
public void setDateFromDialog(Date date)
{
String newDate = getFormattedDate(date);
updateTodaysDate();
mLastSelectedDate = date;
setupViewPagerAdapter(getViewPagerPos());
if(newDate.equals(mTodaysDate))
mDateTextView.setText("Today");
else
mDateTextView.setText(newDate);
}
private int getViewPagerPos()
{
return mViewPager.getCurrentItem();
}
}
The 2 buttons mPrevDateButton and mNextDateButtonare the buttons that when clicked "fix" the problem. Inside their click listeners, the updateDate() method is only doing stuff related to a TextView object and is why I didn't include the definitions (I'm trying to keep the post as short as possible). So I know for sure it has something to do with my setupViewPagerAdapter() method. I've tried adding this method in onCreate() and onResume() but the Fragments still stay blank.
Here is my ViewPager adapter code:
public class MainPagerAdapter extends FragmentPagerAdapter
{
private final String[] mFragmentTitleList = {"My Games", "All Games", "All Places", "My Places"}; //tab title names
private final static int NUM_FRAGMENTS = 4;
public MainPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch(position)
{
case 0:
return MyGamesFragment.newInstance();
case 1:
return AllGamesFragment.newInstance();
case 2:
return AllPlacesFragment.newInstance();
case 3:
return MyPlacesFragment.newInstance();
default:
return null;
}
}
#Override
public int getCount()
{
return NUM_FRAGMENTS;
}
#Override
public CharSequence getPageTitle(int position)
{
return mFragmentTitleList[position];
}
}
Here is one of the 4 fragments. They pretty much all have similar design.
public class AllGamesFragment extends Fragment
{
private SwipeRefreshLayout mSwipeRefreshLayout;
private RecyclerView mAllGamesRecyclerView;
private GameAdapter mGameAdapter;
private List<Game> mGameList = new ArrayList<>();
private MainActivity mMainActivity; //keeps a reference to MainActivity to access public methods
public static AllGamesFragment newInstance()
{
return new AllGamesFragment();
}
#Override
public void onAttach(Context context)
{
super.onAttach(context);
mMainActivity = (MainActivity) context;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
mMainActivity.getProgressBar().setVisibility(View.VISIBLE);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.recycler_view_all_games, container, false);
mAllGamesRecyclerView = (RecyclerView) view.findViewById(R.id.all_games_recycler_view);
mAllGamesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
#Override
public void onRefresh()
{
getGamesData();
mSwipeRefreshLayout.setRefreshing(false);
}
});
return view;
}
#Override
public void onResume()
{
super.onResume();
getGamesData();
}
private void setupAdapter()
{
if(isAdded())
{
mGameAdapter = new GameAdapter(mGameList);
mAllGamesRecyclerView.setAdapter(mGameAdapter);
}
}
private void getGamesData()
{
new GetDataTask(mMainActivity.getLastSelectedDate()).execute();
}
private class GetDataTask extends Scraper.GameDataTask
{
GetDataTask(Date date)
{
super(date, getActivity());
}
#Override
protected void onPostExecute(List<Game> games)
{
mGameList = games;
setupAdapter();
mMainActivity.getProgressBar().setVisibility(View.GONE);
}
}
private class GameAdapter extends RecyclerView.Adapter<GameHolder>
{
private List<Game> mGames;
public GameAdapter(List<Game> games)
{
mGames = games;
}
#Override
public GameHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.list_game_item, parent, false);
return new GameHolder(view, getActivity());
}
#Override
public void onBindViewHolder(GameHolder holder, int position)
{
holder.bindGameData(mGames.get(position));
}
#Override
public int getItemCount()
{
return mGames.size();
}
}
I've looked at so many posts on here, changed my code many times and I still can't get it to work. I'm just baffled that it "fixes" when I click one of the date buttons. If you would like me to post more code of something let me know. Sorry for the long post but I feel the context is needed. I really appreciate any help!
I figured it out! I had to switch to a FragmentStatePagerAdapter rather than use a FragmentPagerAdapter. I believe it has something to do with the Fragments being destroyed and recreated again.
I also read that another way would be to wrap the ViewPager in a Fragment and when you create its adapter you use getChildFragmentManager() rather than getSupportFragmentManager() like this:
mAdapterViewPager = new MyPagerAdapter(getChildFragmentManager());
I didn't want to have to add an entire Fragment around my ViewPager just to fix this issue so I switched to using FragmentStatePagerAdapter and it worked! Hope this helps someone else out there.
Making viewPager adapter extends FragmentStatePagerAdapter and not FragmentPagerAdapter should solve the problem.

Android how to make a thread wait until a certain event happens?

[My condition]
Now in MainActivity, I create some Fragments in which there are some TextViews. And there's an asynchronous thread AsyncTask in MainActivity to obtain information from web. After AsyncTask finished obtaining it will update the text in TextViews mentioned above through callback method (The Fragment implements a OnInfoGotInterface and instanciate a method onInfoGot() in the interface. onInfoGot() will call the method setTextView() defined in the Fragment to update information).
[My problem]
When execute the program, I found that the time point when AsyncTask finishes get information from web precedes the time point when Fragment call onCreateView(). In another word, when AsyncTask call (OnInfoGotInterface)fragment.onInfoGot() to set the TextViews, the TextViews have not been instanciated yet (TextViews are instanciated in on CreateView() by rootView.findViewById() method). As a result, a NullPointerException showed up.
[I need a resolution]
Now I want to do like this: When AsyncTask finishes getting info from web and is going to call onInfoGot(), we stop it, and make it wait until the Fragment finish onCreateView(). After that we waken the AsyncTask and allow it update the fragment.
PS. Some suggest that I should call new AsyncTask.excute() after onCreateView() in the definition of Fragment. But here AsyncTask and Fragment are both created in MainActivity. They are two differnt task threads in MainActivity, one is used for showing data and the other is used for getting data from Web.
Could anyone give some advice? I would really appreciate it!
Here is the code:
MainActivity.java:
public class MainActivity extends Activity{
private List<City> cityList;
private ViewPager mPager; /*ViewPager for show the page of city info*/
private ScreenSlidePagerAdapter mPagerAdapter; /* Adapter for the ViewPager,
* ScreenSlidePagerAdapter is a subclass of FragmentStatePagerAdapter*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_city);
cityList = new ArrayList<City>();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
init();
}
private void init(){
loadCities(); //Load cities from database into cityList
initFragments();
getCityInfo();
}
private void initFragments(){
mPagerAdapter.removeAllFragments();
for(City city: cityList){
PageFragment fragment = new PageFragment(city.getName());
mPagerAdapter.addFragment(fragment);
mPagerAdapter.notifyDataSetChanged();
}
}
private void getCityInfo(){
for(City city: cityList){
String cityName = city.getName();
obtainCityInfo(cityName);
}
}
private obtainCityInfo(String cityName){
String request = "http://example.abc.com/"
+ cityName + "&output=json";
new AccessWebServiceTask().execute(request, cityName);
}
private class AccessWebServiceTask extends AsyncTask<String, Void, CityInfo>{
#Override
protected CityInfo doInBackground(String... urls) {
String result = getWebContent(urls[0]); /*Access web through HTTP*/
String cityName = urls[1];
/*Transform the String result to a CityInfo object containing information of a city*/
CityInfo cityInfo = encodeJason(result, cityName);
return cityInfo;
}
protected void onPostExecute(CityInfo cityInfo){
OnCityGot(cityInfo);
}
}
public void onCityGot(CityInfo cityInfo){
if(cityInfo != null){
String cityName = cityInfo.getCityName();
/*Set the info field of a city object*/
cityList.getByName(cityName).setCityInfo(cityInfo);
mPagerAdapter.updateFragment(cityInfo);
}
}
}
ScreenSlidePagerAdapter.java
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
private List<PageFragment> fragmentsList;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
fragmentsList = new ArrayList<PageFragment>();
}
public ScreenSlidePagerAdapter(FragmentManager fm, List<PageFragment> list){
super(fm);
fragmentsList = list;
}
#Override
public PageFragment getItem(int position) {
return fragmentsList.get(position);
}
#Override
public int getCount() {
return fragmentsList.size();
}
public void setFragmentsList(List<PageFragment> fragmentsList){
this.fragmentsList = fragmentsList;
notifyDataSetChanged();
}
public void addFragment(PageFragment f){
fragmentsList.add(f);
notifyDataSetChanged();
}
public void removeFragment(int position){
fragmentsList.remove(position);
notifyDataSetChanged();
}
public void removeAllFragments(){
fragmentsList.clear();
notifyDataSetChanged();
}
private PageFragment findFragmentByName(String cityName){
for(PageFragment fragment: fragmentsList){
if(fragment.getCityName().equals(cityName))
return fragment;
}
return null;
}
public void updateFragment(CityInfo cityInfo){
String cityName = cityInfo.getCityName();
OnCityInfoChanged fragment = (OnCityInfoChanged)findFragmentByName(cityName);
String population = cityInfo.getPopulation();
fragment.onCityInfoChanged(population);
notifyDataSetChanged();
}
}
PageFragment.java:
public class PageFragment extends Fragment implements OnCityInfoChanged{
private TextView cityNameText;
private TextView populationText;
String cityName;
String population;
public PageFragment(){}
public PageFragment(String cityName, String population){
this.cityName = cityName;
this.population = population
}
public PageFragment(CityInfo cityInfo){
this.cityName = cityInfo.getCityName();
this.population = cityInfo.getPopulation();
}
public PageFragment(String cityName){
this.cityName = cityName;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
setCityName(cityName);
setPopulation(population)
return rootView;
}
public void setCityName(String name){
cityNameText.setText(name);
}
public void setPopulation(String population){
populationText.setText(population);
}
public String getCityName(){
return cityName;
}
#Override
public void onCityInfoChanged(String population) {
//setCityName(cityName);
setPopulation();
}
}
I would advise to remove the AsyncTask AccessWebServiceTask from the MainActivity and put it in the Fragment PageFragment. And then in this fragment, override onActivityCreated, start the AsyncTask.
[EDIT]
Here is an update version of your code. Test it if it works :
MainActivity
public class MainActivity extends Activity{
private List<City> cityList;
private ViewPager mPager; /*ViewPager for show the page of city info*/
private ScreenSlidePagerAdapter mPagerAdapter; /* Adapter for the ViewPager,
* ScreenSlidePagerAdapter is a subclass of FragmentStatePagerAdapter*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_city);
cityList = new ArrayList<City>();
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
init();
}
private void init(){
loadCities(); //Load cities from database into cityList
initFragments();
}
private void initFragments(){
mPagerAdapter.removeAllFragments();
for(City city: cityList){
PageFragment fragment = PageFragment.newFragment(city.getName());
mPagerAdapter.addFragment(fragment);
}
}
}
ScreenSlidePagerAdapter :
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
private List<PageFragment> fragmentsList;
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
fragmentsList = new ArrayList<PageFragment>();
}
public ScreenSlidePagerAdapter(FragmentManager fm, List<PageFragment> list){
super(fm);
fragmentsList = list;
}
#Override
public PageFragment getItem(int position) {
return fragmentsList.get(position);
}
#Override
public int getCount() {
return fragmentsList.size();
}
public void setFragmentsList(List<PageFragment> fragmentsList){
this.fragmentsList = fragmentsList;
notifyDataSetChanged();
}
public void addFragment(PageFragment f){
fragmentsList.add(f);
notifyDataSetChanged();
}
public void removeFragment(int position){
fragmentsList.remove(position);
notifyDataSetChanged();
}
public void removeAllFragments(){
fragmentsList.clear();
notifyDataSetChanged();
}
}
PageFragment :
public class PageFragment extends Fragment {
private TextView cityNameText;
private TextView populationText;
private String cityName;
private String population;
public static final String CITY_NAME_KEY = "cityname";
public PageFragment(){}
public static PageFragment newFragment(String cityName){
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(CITY_NAME_KEY, cityName);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate();
if(savedInstanceState != null){
this.cityName = savedInstanceState.getString(CITY_NAME_KEY);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
if(this.cityName != null){
String request = "http://example.abc.com/" + cityName + "&output=json";
new AccessWebServiceTask().execute(request, cityName);
}
}
public void setCityName(String name){
cityNameText.setText(name);
}
public void setPopulation(String population){
populationText.setText(population);
}
public String getCityName(){
return cityName;
}
private class AccessWebServiceTask extends AsyncTask<String, Void, CityInfo>{
#Override
protected CityInfo doInBackground(String... urls) {
String result = getWebContent(urls[0]); /*Access web through HTTP*/
String cityName = urls[1];
/*Transform the String result to a CityInfo object containing information of a city*/
CityInfo cityInfo = encodeJason(result, cityName);
return cityInfo;
}
protected void onPostExecute(CityInfo cityInfo){
OnCityGot(cityInfo);
}
}
public void onCityGot(CityInfo cityInfo){
if(cityInfo != null){
String population = cityInfo.getPopulation();
setPopulation(population);
}
}
}
You can try ConditionVariable, it can be used to hold AsyncTask
private ConditionVariable mCondition = new ConditionVariable(false);
mCondition.block(); //used to block
// your condition
mCondition.open(); // used to open
First, you shouldn't use Fragment constructors with parameters. You should send the arguments to the fragment in bundle set to the fragments arguments. See: https://stackoverflow.com/a/15392591/360211
I would have the fragment call the obtainCityInfo method in it's onCreate. And no more lookups then, I can pass the OnCityInfoChanged to the async task for it to call on the onPostExecute.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_city_page2, container, false);
cityNameText = (TextView)rootView.findViewById(R.id.city_name);
populationText = (TextView)rootView.findViewById(R.id.population);
setCityName(cityName);
obtainCityInfo(cityName, this);
return rootView;
}
private obtainCityInfo(String cityName, OnCityInfoChanged callback){
String request = "http://example.abc.com/"
+ cityName + "&output=json";
new AccessWebServiceTask(callback).execute(request, cityName);
}

How to add custom view in ViewPager for walkthrough

I've used ViewPagerIndicator to get WalkThrough like most popular application. But I can't understand how to add Pictures in ViewPager which shows how to use the application.
What I want is like these Walk Through.
What i got till now.
I dunno
How to add Custom View like ImageView and TextView in ViewPager?
Any guidance would be most welcome.
Since the question really old, I'll just write a short general guideline:
To populate viewpager with content, you add fragments to an adapter and then you set the adapter to the viewpager. You can do pretty much any layout in the fragment, putting there images/text etc..
As Inteist suggest, one can put any layout in fragment and supply that fragment to adapter.
Fragment:
public final class SelectModelFragment extends Fragment {
private static final String KEY_CONTENT = "SelectModelFragment:Content";
private static String TAG = SelectModelFragment.class.getSimpleName();
private SelectModel mSelectModelObj;
private CircularImageView mImageView;
public static SelectModelFragment newInstance(SelectModel obj) {
SelectModelFragment fragment = new SelectModelFragment();
fragment.mSelectModelObj =obj;
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mSelectModelObj = savedInstanceState.getParcelable(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_select_model, container, false);
mImageView = (CircularImageView)view.findViewById(R.id.fragment_select_model_iv);
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(KEY_CONTENT, mSelectModelObj);
}
}
Fragment Adapter :
public class SelectModelAdapter extends FragmentPagerAdapter {
ArrayList<SelectModel> mList;
private int mCount;
private static final String TAG = SelectModelAdapter.class.getSimpleName();
public SelectModelAdapter(FragmentManager fm, ArrayList<SelectModel> mList) {
super(fm);
this.mList = mList;
mCount = mList.size();
}
#Override
public Fragment getItem(int position) {
return SelectModelFragment.newInstance(mList.get(position));
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position) {
return TAG;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
Activity: Where ViewPager has Fragment Adapter which feeds Fragment.
public class SelectModelActivity extends BaseSliderActivity {
private ViewPager mPager;
private SelectModelAdapter mAdapter;
private ArrayList<SelectModel> mList;
private void setAdapter() {
mAdapter = new SelectModelAdapter(getSupportFragmentManager(), mList);
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
}
}

What is correct way to init views of fragments while using ViewPager?

My goal is init views in main activity, but I got nullpointerexception.
Here my sample code:
public class MainActivity extends SherlockFragmentActivity {
private static final String TAG = "MainActivity";
Fragment one, two, three;
List<Fragment> fragments;
ViewPager pager;
private MyPagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialisePaging();
initTwo ();
}
private void initialisePaging() {
Log.d(TAG, "initialisePaging");
fragments = new Vector<Fragment>();
one = Fragment.instantiate(getApplicationContext(), One.class.getName());
two = Fragment.instantiate(getApplicationContext(), Two.class.getName());
three = Fragment.instantiate(getApplicationContext(), Three.class.getName());
fragments.add(one);
fragments.add(two);
fragments.add(three);
mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments) ;
pager = (ViewPager) findViewById(R.id.trinity_viewpager);
pager.setAdapter(mPagerAdapter);
}
private void initTwo (){
View twoView = mPagerAdapter.getItem(1).getView();
TextView twoTv = (TextView) twoView.findViewById(R.id.frag_two_tv);
twoTv.setText("2!!!!");
}
class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return 3;
}
}
}
Fragments One, Two and Three contains only textViews. I try to init these views and got
NullPointerException:
E/AndroidRuntime(2104): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2104): at ru.alxr.viewpagerapp.MainActivity.initTwo(MainActivity.java:53)
E/AndroidRuntime(2104): at ru.alxr.viewpagerapp.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(2104): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(2104): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(2104): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Try inflating the view before using findViewById.
UPDATED
I found way to solve problem.
The simpliest way is pass vies via interface:
here is adapter:
static class MyPagerAdapter extends FragmentPagerAdapter {
public static MyPagerAdapter instance = null;
public static MyPagerAdapter getInstance(FragmentManager fm){
if (instance == null) instance = new MyPagerAdapter(fm);
return instance;
}
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment r = null;
switch (position){
default : break;
case 0 : {
r = One.newInstance();
break;
}
case 1 : {
r = Two.newInstance();
break;
}
case 2 : {
r = Three.newInstance();
break;
}
}
return r;
}
#Override
public int getCount() {
return 3;
}
}
and one of fragments:
public final class One extends SherlockFragment {
ViewOneCreated viewOneCreated;
static One one;
Button button;
ExpandableListView someListView;
Map <String, Object> classOneObjects;
private static final String
FRAGMENT = "FRAGMENT",
LISTVIEW = "LISTVIEW",
BUTTON = "BUTTON";
public interface ViewOneCreated{
public void fragmentOneCreated(Map <String, Object> classOneObjects);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
viewOneCreated = (ViewOneCreated) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onSomeEventListener");
}
}
public static One instance() {
if (one == null) one = new One();
return one;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View baseView = inflater.inflate(R.layout.one_fragment, null);
button = (Button) baseView.findViewById(R.id.fragment_one_translate_button);
someListView= (ExpandableListView) baseView.findViewById(R.id.one_fragment_exp_list_view);
return baseView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
classOneObjects = new TreeMap<String, Object>();
classOneObjects .put(FRAGMENT, (SherlockFragment) this);
classOneObjects .put(BUTTON, button);
classOneObjects .put(LISTVIEW, someListView);
viewOneCreated.fragmentOneCreated(classOneObjects);
}
//...
}

Categories

Resources