-1

ANSWER: Okey great thx from the master here uDevel convincing me that I was going a very wrong way. I have found a similar problem that saved my ass and took me about 10 to get my shit to gather... here you go: How to implement a ViewPager with different Fragments / Layouts

PROBLEM:

I know there a few question about fragmenst here already but I didn´t find my answer there.

I am trying to connect three fragments to CalendarActivity. I think it's best to explain in code.

THE ERROR I GET IS: http://postimg.org/image/aca9ndfdb/ I have tried many different imports but I can't image how to implement this so would appreciate some help. If I remove android.app when I create a fragmentMananger/fragmentTransaction I get: http://postimg.org/image/dy2av2zp9/

Here is CalendarActicity

package ru.calendar;

import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import jBerry.MySugar.R;

public class CalendarActivity extends FragmentActivity implements TabListener  {

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_calendar);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        FragmentA frag = new FragmentA();
        fragmentTransaction.replace(android.R.id.content, frag);

        fragmentTransaction.commit();

        ActionBar actionBar;
        ViewPager viewPager;

        viewPager = (ViewPager) findViewById(R.id.calendarContainer);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                actionBar.setSelectedNavigationItem(arg0);
                // Intent intent = new Intent(this, CalendarActivity.class);
                // startActivity(intent);
                Log.d("DpoiNT", "onPageSelected at " + " position " + arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                //    Log.d("DpoiNT", "onPageScrolled at "+" position " +arg0+" from " +arg1+" with number of pixels "+arg2);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if(arg0== ViewPager.SCROLL_STATE_IDLE){
                    Log.d("DpoiNT", "onPageScrollStateChanged Idle");
                }
                if(arg0== ViewPager.SCROLL_STATE_DRAGGING){
                    Log.d("DpoiNT", "onPageScrollStateChanged Dragging");
                }
                if(arg0== ViewPager.SCROLL_STATE_SETTLING){
                    Log.d("DpoiNT", "onPageScrollStateChanged Settling");
                }
            }
        });

    actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1=actionBar.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2=actionBar.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);

    ActionBar.Tab tab3=actionBar.newTab();
    tab3.setText("Tab 3");
    tab3.setTabListener(this);

    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
    actionBar.addTab(tab3);
}


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabSelected at "+" position " +tab.getPosition()+" name "+tab.getText());
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabUnselected at "+" position " +tab.getPosition()+" name "+tab.getText());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Log.d("DpoiNT", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());
    }
}


class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;
        if(arg0 == 0) {
            fragment = new FragmentA();
        }

        if(arg0 == 1) {
        fragment = new FragmentB();
        }

        if(arg0 == 2) {
            fragment = new FragmentC();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

Here is the xml for CalendarActivity (activity_calendar):

<android.support.v4.view.ViewPager
<!-- ViewPager for swipe function in CalendarActivity -->

    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"
    tools:context="ru.calendar.CalendarActivity"
    android:id="@+id/calendarContainer">

    <fragment
        android:id="@+id/fragment_a"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#ffffff"
        class="ru.calendar.FragmentA" />

    <fragment
        android:id="@+id/fragment_b"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        class="ru.calendar.FragmentB" />

    <fragment
        android:id="@+id/fragment_c"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        class="ru.calendar.FragmentC" />

</android.support.v4.view.ViewPager>

And this is how one fragment.xml looks like:

public class FragmentA extends Fragment {

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }
}

EDIT This a part of CalendarActivity below...

    viewPager = (ViewPager) findViewById(R.id.calendarContainer);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            actionBar.setSelectedNavigationItem(arg0);
            // Intent intent = new Intent(this, CalendarActivity.class);
            // startActivity(intent);
            Log.d("DpoiNT", "onPageSelected at " + " position " + arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            //    Log.d("DpoiNT", "onPageScrolled at "+" position " +arg0+" from " +arg1+" with number of pixels "+arg2);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            if(arg0== ViewPager.SCROLL_STATE_IDLE){
                Log.d("DpoiNT", "onPageScrollStateChanged Idle");
            }
            if(arg0== ViewPager.SCROLL_STATE_DRAGGING){
                Log.d("DpoiNT", "onPageScrollStateChanged Dragging");
            }
            if(arg0== ViewPager.SCROLL_STATE_SETTLING){
                Log.d("DpoiNT", "onPageScrollStateChanged Settling");
            }
        }
    });


class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;
        if(arg0 == 0) {
            fragment = new FragmentA();
        }

        if(arg0 == 1) {
            fragment = new FragmentB();
        }

        if(arg0 == 2) {
            fragment = new FragmentC();
        }

        return fragment;
    }

    @Override
    public int getCount() {
    return 3;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sindri Þór
  • 2,887
  • 3
  • 26
  • 32

2 Answers2

0

You are using the Fragments from the support library. For them you have to uses the SupportFragmentManager. You get it by calling

getSupportFragmentManager() 

instead of

getFragmentManager()
Simon Meyer
  • 1,946
  • 12
  • 23
  • Yes perherps you are right, i did use getSupportFragment() at some point, i changed it somewhere in the debuggin process of mine lol.. – Sindri Þór Jul 11 '14 at 17:53
  • Here is the error i get by having support in this [click me](http://postimg.org/image/uxvxnt0ox/) – Sindri Þór Jul 11 '14 at 18:02
0

This is not how you do view pager with fragment; you need use PagerAdapter, and you don't define your fragment in xml, it should be dynamically instantiated in code by the adapter.

http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html http://developer.android.com/training/animation/screen-slide.html

This part of code is only used for default layout and single fragment.

android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
FragmentA frag = new FragmentA();
fragmentTransaction.replace(android.R.id.content, frag);

fragmentTransaction.commit();

Just follow the guide for view pager.

Or if you want to have more than 1 fragment in same activity, and on same screen at the same time, then you don't use view pager. You need a root viewgroup with few viewgroups with id in the main xml and use the code you got above.

...
fragmentTransaction.replace([ID of the view group you want your fragment to be in], frag);
...

You have make up your mind what you want to do first.

uDevel
  • 1,891
  • 1
  • 13
  • 14
  • Yes you are correct sir, i do use it in the CalendarActivity. Do you have any other ideas like am i using it correctly? – Sindri Þór Jul 11 '14 at 17:20
  • What do you use? View pager? or multiple fragment on same page at the same time? For viewpager, http://developer.android.com/training/animation/screen-slide.html gives you 99% implementation of viewpager. – uDevel Jul 11 '14 at 17:27
  • So, you got rid of this: android.app.FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); FragmentA frag = new FragmentA(); fragmentTransaction.replace(android.R.id.content, frag); fragmentTransaction.commit(); Right? Show your MyAdapter. – uDevel Jul 11 '14 at 17:38
  • I do use ViewPager becouse i want to swipe between fragments. I put some more code to show what im thinking under EDIT.. – Sindri Þór Jul 11 '14 at 17:51
  • Did you get rid of this: android.app.FragmentManager fragmentManager = getFragmentManager(); android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); FragmentA frag = new FragmentA(); fragmentTransaction.replace(android.R.id.content, frag); fragmentTransaction.commit(); This piece of code has nothing to do with viewpager. – uDevel Jul 11 '14 at 17:59
  • No i didn´t want to delete that piece of code becouse i wanted to upload it onCreate. If you understand me right: I have CalendarActivity and there i want to have a manager that handles my fragments classes. under each fragment class is a fragment.xml. If i get rid of that code i will not be able to handle my fragments classes. Or am i thinking this wrong? – Sindri Þór Jul 11 '14 at 18:08
  • I have edited the CalendarActivity code at the top: pasted the whole class for you to see better.. – Sindri Þór Jul 11 '14 at 18:18
  • So, you want to have a fragment (X) AND a viewpager of fragments (A,B,C) on the same page below (X)? Either way, that piece of code is WRONG. You need to use supportfragmentmanager for (X), and need to define a container for it; you can't use android.R.id.content. Your activity_calendar.xml only has a viewpager as a root, it doesn't have container for (x). For the view pager, do not define your fragments in the xml. It should be empty. You are doing too much with so many wrongs. Can you restart by just creating the viewpager for (A,B,C) following the guideline first? then build from there. – uDevel Jul 11 '14 at 18:18
  • Thank you for spanking me around, i needed that.. I have edit my post and put the answer for my proplem at the top of the question here.. cheers! – Sindri Þór Jul 11 '14 at 23:08