0

I am trying to change textview in an xml page that impliments android's swipe menu. I tried putting the code in main activity's oncreate method but it crashes upon start up. I do have another class but it calls oncreateview instead of oncreate and the findviewbyid method doesn't exist in oncreateview().

I implented android's swipe menu when creating this app. It has two pages, an input page and output page. I have filled the input page with textviews, edittexts, and buttons. Im not sure where to change the textview. My goal is to set the textview to todays date.

ViewPager mViewPager;

@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 (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

  TextView tv = (TextView) findViewById(R.id.textView4);
 // tv.setText("test post");
//      tv.setText("test post");
}

public class inputClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.input_xml, container, false);

    return rootView;
}
}
Jayizzle
  • 532
  • 1
  • 6
  • 24

2 Answers2

1

As I understand you want to change text in your fragment. You should handle everything about stuff in your fragments in your fragment class. So you should change it in your fragment class' onCreateView method or you can store your rootView and do any changes like that later.

((TextView) rootView.findViewById( R.id.yourId )).setText( "Hello from Turkiye" );
Arda Kara
  • 501
  • 6
  • 24
  • It also works if I put the changetext code in the onCreateOptionsMenu thats past the code in onCreate. – Jayizzle Aug 19 '14 at 19:56
  • Android is flexible in some points. For example you inflate your menu anywhere after the onCreateOptionsMenu method is called. But it is suggested to do set your menü in that method. There are suggested ways to do stuff and you can go out of the rules of course. There are various ways to handle fragments. One is passing the rootView to activity, store it in your activity class and work on it there. But it is not suggested. You should handle your fragment in your fragment activity. – Arda Kara Aug 19 '14 at 20:03
1

With regards to this satement:

I do have another class but it calls oncreateview instead of oncreate and the findviewbyid method doesn't exist in oncreateview().

You can pass your main activity's Context to your other class through it's constructor, and call the findViewById method by casting the Context to Activity. Something like this in your class.

Context mContext;

public yourconsructor(Context context)
     {
     mContext = context;
     }

View view = ((Activity)mContext).findViewById(R.id.yourview)

More here too:

using findviewbyid in a class that does NOT extend Activity in android

Community
  • 1
  • 1
Will Thomson
  • 875
  • 6
  • 19