JZweige
JZweige

Reputation: 731

fragment.getview() returning null from activity

I have a viewpager that hosts several fragments. One of them, Frag1, is supposed to recieve an object with data and poplulate some EditText with that data.

The thing is, when I try to access the fragment from the activity, getView() always returns null, making it impossible to call methods of the Frag1 class. I am using LogCat to check if other variables are null, but it seems that the problem is coming from the getView() check.

Any idea why it is null?

Activity code

   (...)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form1);

        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        final ActionBar actionBar = getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

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

public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new Frag1();

                case 1:
                    return new Frag2();

                case 2:
                    return new Frag1();

                case 3:
                    return new Frag1();

                default:
                    return new Frag1();
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }


    public void Populate(Patient patient) {
        Frag1 fragment = 
                  (Frag1) mAppSectionsPagerAdapter.getItem(0);

        Log.d(patient.getName(), "data check");
        Log.d(fragment.hello(), "fragment check");

        if (fragment.getView() == null) {
            Log.d("*** DEBUG ***", "found the problem champ");
        }

              if(fragment != null)  // could be null if not instantiated yet
              {
                 if(fragment.getView() != null) 
                 {
                    // no need to call if fragment's onDestroyView() 
                    //has since been called.
                    fragment.setName(patient.getName());
                    Log.d(patient.getName(), "supbrah");// do what updates are required
                 }
              }
    }

Fragment code

public class Frag1 extends Fragment {

EditText name;
Spinner spinner;
AutoCompleteTextView textView;
AutoCompleteTextView textView1;
Spinner spinner1;
AutoCompleteTextView textView2;
View v;

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

    name = (EditText) v.findViewById(R.id.name_edit);

    return v;
}

public String hello() {
    return "sup brah";
}

public String getName() {
    String patient = name.getText().toString();
    return patient;
}

public void setName(String text) {
    name.setText(text, TextView.BufferType.EDITABLE);
}

}

Upvotes: 2

Views: 8951

Answers (2)

LordMarty
LordMarty

Reputation: 1591

Correct me if I'm wrong, but shouldn't you use getView().findViewById(R.id.name_edit); and put it in onViewCreated? The way it is now shouldn't work imo.

Here's some samplecode:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    if (container == null) {
        return null;
    }
    return inflater.inflate(R.layout.activity_form, container, false);

}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

name = (EditText) getView().findViewById(R.id.name_edit);

}

Hope it helps. :)

Upvotes: 5

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

Try calling viewPager.getChildAt(int x); instead of viewPagerAdapter.getItem(int x).getView();

Upvotes: 2

Related Questions