Tabbed Activity of Error

Asked

Viewed 66 times

2

Hello, I’ll be as objective as possible:

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

  • By app options(right mouse) New -> Fragment -> Fragment Blank created a Fragment called "Page"

In this the Android Studio itself generated for 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 mistake:

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)

So as not to get too strenuous I 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 Android Studio that created.

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

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;
}

}

Then I created two classes extending from Fragment:

Tabframent1:

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, the 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 its 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" />

  • Obg! But how do I pass values from one fragment to another? .-.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.