Error returning in getView method

Asked

Viewed 65 times

2

I’m doing the app in Action Bar and I have a problem returning the value within the method getItem. Here’s the code:

Java test.

public class Teste extends Fragment
{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.notas, container, false);

    TextView tx = (TextView) v.findViewById(R.id.edtTeste);
    tx.setText("Testando");
    // TODO Auto-generated method stub
    return v;
}


}

Mainactivity.java

    public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;


    ViewPager mViewPager;

    private String[] tabTitles = {"Teste", "Tela Principal", "Notas"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.

            for(String tab_name : tabTitles) {


            Tab tab = actionBar.newTab();
            tab.setTabListener(this);
            tab.setText(tab_name);

            actionBar.addTab(tab);
            }
        /*  actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));*/
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    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 DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            //Fragment fragment = new Teste();

        switch(position)
        {
        case 0:
            return  new Nota();
            default: return null;
        }

            //return null;


        }

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


    }



}
  • What error/problem is happening?

  • within the getItem method, it fails to return to the Test class.

  • You’re really waiting for class Teste or the Nota?

  • Test class.... I modified in the code

1 answer

1

Try to instantiate the Fragment so, instead of just giving a new:

return Fragment.instantiate(context, Teste.class.getName())

In his Activity, the context is the this, if you do not have this property for another use.

Browser other questions tagged

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