Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when calling findViewById() to get a Fragment's View

I'm trying to setup a FragmentStatePagerAdapter to create a scrollable tab interface on one of my fragments similar to this example. In my activity's onCreate:

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.base);

     setVersionString();
     setupActionBar();
     setupNavigationDrawer();
     setupFragments();
}

I set the content view to the following layout (R.layout.base):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BaseActivity" >

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/nav_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#FFF" />

</android.support.v4.widget.DrawerLayout>

I then call the following function:

private void setupFragments() {
    // Check that the activity is using the correct layout
    if (findViewById(R.id.main_frame) != null) {
        // Initialize the first fragment
        MainFragment firstFrag = new MainFragment();

        // Pass any extras from an intent to the Fragment
        firstFrag.setArguments(getIntent().getExtras());

        // Add the first Fragment to the screen
        getSupportFragmentManager().beginTransaction()
            .add(R.id.main_frame, firstFrag).commit();

        tabsAdapter = firstFrag.getAdapter();
        Log.d("Base.LOG", "tabsAdapter = " + tabsAdapter);
        tabsAdapter.addTab(DummyFragment.class, null);
    }
}

In an attempt to set a FragmentStatePagerAdapter as the adapter for the view in the fragment's layout (R.layout.main):

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainFragment" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>

And my fragment's code looks lie this:

private View mView;
private TabbedPagerAdapter mTabPageAdapter;
private ViewPager mTabPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d("Main.LOG", "onCreate called");
    mView = inflater.inflate(R.layout.main, container, false);
    Log.d("Main.LOG", "Layout inflated");
    setupTabs();
    Log.d("Main.LOG", "Tabs setup");

    return mView;
}

@Override
public void onResume() {
    super.onResume();
    Log.d("Main.LOG", "onResume called");
}

/** Set up the ViewPager and Adapter to handle the primary tabs */
private void setupTabs() {
    // Initialize the adapter
    mTabPageAdapter = new TabbedPagerAdapter(getActivity(),
            getActivity().getSupportFragmentManager());

    // Initialize the view pager
    mTabPager = (ViewPager) mView.findViewById(R.id.main_page);
    Log.d("Main.LOG", "mTabPager = " + mTabPager);
    Log.d("Main.LOG", "mTabPageAdapter = " + mTabPageAdapter);
    mTabPager.setAdapter(mTabPageAdapter);
}

/* Accessor for TabbedPagerAdapter */
public TabbedPagerAdapter getAdapter() {
    return mTabPageAdapter;
}

The app force closes when I try to add a tab with a NullPointerException. The debug messages in the log show that:

D/Base.LOG(27173): tabsAdapter = null

is displayed before any of the fragment's debug messages. How can I ensure that the layout is inflated before trying to access any of it's Views or subclasses?

like image 886
embedded.kyle Avatar asked Oct 22 '22 01:10

embedded.kyle


2 Answers

As per my comment

public class MainFragment extends Fragment
{
    private MainInterface mInterface = null;
    //make sure to set the above somehow

    public interface MainInterface{
        public void onFragmentReady(.....);
    }

    ... //Do regular stuff here

    public void onResume()
    {
        //Do what you need to do in onResume
        if(mInterface != null)
        {
            mInteface.onFragmentReady();
        }
    }
}

This should allow you to know when the fragment is ready and you can do your getTabAdapter calls then.

like image 61
Raigex Avatar answered Oct 27 '22 11:10

Raigex


Are you calling setContentView(...) inside the onCreate(...) method of your Activity before calling setupFragments(...)?

If yes: Does the layout you are assigning to the Activity contain the ViewPager?

My recommendation: Since you are obviously using a ViewPager inside a Fragment, do the initialization of the ViewPager inside the Fragments onCreateView(...) method.

Example:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // ASSUMING your layout.main contains the ViewPager
    View v = inflater.inflate(R.layout.main, container, false);

    ViewPager pager = (ViewPager) v.findViewById(R.id.pager);
    // and so on ...

    return v;
}
like image 43
Philipp Jahoda Avatar answered Oct 27 '22 11:10

Philipp Jahoda