Tabbed Error Activity

2

Hello, I'll be as objective as possible:

  • I created a project in Android Studio for "Tabbed Activity" - Action Bar Tabs

  • By the Options app (Mouse right) New - > Fragment - > Fragment Blank I created a Fragment named "Page"

In this the Android Studio itself generated to min things, then I did:

ActivityMain.java

//Um Pouco de codigo aqui em cima..
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //Mudei aqui--------------------------------------------------
        //return PlaceholderFragment.newInstance(position + 1);
        return  Page.newInstance("Instancia 1","Fragmento 1");
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }
}

I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: com.lmr.test, PID: 13447
                                                          java.lang.RuntimeException: com.lmr.test.MainActivity@b2a0638 must implement OnFragmentInteractionListener
                                                              at com.lmr.test.Page.onAttach(Page.java:83)
                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
                                                              at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:926)
                                                              at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:876)
                                                              at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
                                                              at android.support.v4.view.ViewPager.populate(ViewPager.java:1175)
                                                              at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                              at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
                                                              at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                              at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                              at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                              at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                              at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                              at android.view.View.measure(View.java:18788)
                                                              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                              at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                              at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                              at android.os.Looper.loop(Looper.java:148)
                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

In order not to get too static put the code in the Github

What I do not understand is what I did wrong, because in theory it should work since it was the Android Studio that created it.

What I'm trying to do and put a separate Fragment for each Tab, I tried several ways but I'm not getting it, so I'm looking for a solution here, Thanks. PS: I know it will only open 1 Fragment, but if it works I create others and use if in getCount();

    
asked by anonymous 14.05.2016 / 03:53

1 answer

0

Well, I'll show you how I did it.

First I created a PageAdapter extending from FragmentStatePagerAdapter as you can see below:

public class PagerAdapter extends FragmentStatePagerAdapter {
int numeroDeAbas;

public PagerAdapter(FragmentManager fm, int numeroDeAbas) {
    super(fm);
    this.numeroDeAbas = numeroDeAbas;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            TabFragment1 tab1 = new TabFragment1();
            return tab1;
        case 1:
            TabFragment2 tab2 = new TabFragment2();
            return tab2;
        default:
            return null;
    }
}

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

}

Next I've created two Fragment extending classes:

TabFrame1:

public class TabFragment1 extends Fragment {

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

}

TaFragment2:

public class TabFragment2 extends Fragment {

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

}

Your respective layouts: Stay on your own!

Finally, MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setTitle("");

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Login"));
    tabLayout.addTab(tabLayout.newTab().setText("Cadastre-se"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

}

And your layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:elevation="6dp"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="@color/colorPrimary"
    app:tabTextColor="@color/black"
    app:tabSelectedTextColor="@color/backgroundColor"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/tab_layout" />

    
03.06.2016 / 15:46