0

I have one activity with 4 fragments. Every fragment got its separate class and layout. This is the parent activity class:

    private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return new MainFragment();
                case 1:
                    return new CatalogFragment();
                case 2:
                    return new FolderFragment();
                case 3:
                    return new SettingsFragment();
                default:
                    return null;
            }
    }

    @Override
    public int getCount() {

        return 4;
    }

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

Fragment classes are only with:

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

and a default constructor. In FragmentMain i have 2 TextViews. I want to change them dynamically depending on info within MainActivity. I am using ViewPager. This is my MainActivity XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />

if i am doing this all wrong please correct me! I can findViewById() but i cannot set the text(NullPointer). From the fragment i cannot even findViewById() even thou they are in the MainFragment XML. I've searched a lot and i cannot find easy to understand solution or explanation how this works. Thanks in advance!

user5804127
  • 49
  • 11
  • You posted a lot of code but you did not post the relevant code of the issue you are having. Where are you calling the findViewById? What is the id you are looking for? Where do you set the text of the TextView? – MikeL Jan 21 '16 at 20:26
  • I tried to set text in `onCreate()` in the Main Activity. This is the place i got the null pointer. After this i tried to set it in Fragment class but there is no `findViewById()` method there. Id i need is ANY of these 3 id-s in the XML posted below all the code. – user5804127 Jan 21 '16 at 20:32
  • Unless you share the problematic code sections it will be hard to the stackoverflow community to help you find the problem. It can be anything from not calling properly to the findViewById, passing the wrong id, casting etc... – MikeL Jan 21 '16 at 20:45
  • It is not my first app. The problem is i dont know the basis of Fragment comunication. I am not using wrong ID's and the method is called properly. Problem is it doesnt exist in the fragent. – user5804127 Jan 21 '16 at 21:30

0 Answers0