Search code examples
javaandroidnullpointerexceptiontextviewsetcontentview

TextView is null after setContentView


I've looked at several similar questions on here, but none of them have explained my problem. I am simply trying to reference a TextView by it's id, but I keep getting Null Pointer Exceptions whenever I try to do anything with it. I made sure to put my assignment after both setContentView() is called and the fragment is inflated. To make sure there wasn't anything else causing problem I removed the other portions of my code and tested for null immediately after assignment. Regardless of this, it's still showing as null.

Here's my code:

public class MainActivity extends Activity
{
    //MEMBER VARIABLES
    TextView mQuestionText;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        mQuestionText = (TextView) findViewById(R.id.questionTextView);
        if (mQuestionText == null)
            System.out.println("debug:: " + "null");
    }

    @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 boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {

        public PlaceholderFragment()
        {
        }

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

And here's the fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.wichita.eecs.cs697ac.w387u669.assignment3.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:text="Question"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/questionTextView"
        android:text="False" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/questionTextView"
        android:layout_marginTop="35dp"
        android:layout_toLeftOf="@+id/questionTextView"
        android:text="True" />

</RelativeLayout>

Thanks for your help!

EDIT: After doing some other research, I learned that I could override the onStart() method and move my code there as I would have access to the views (even those from the fragment). However this is just a bypass that doesn't really use fragments as they are meant to be used.

protected void onStart()
    {
        super.onStart();
        TextView test = (TextView) findViewById(R.id.questionTextView);
        test.setText("hello");
    }

Hope that helps someone


Solution

  • questionTextView TextView is in fragment_main.xml layout of PlaceholderFragment, so use onCreateView of Fragment to initialize TextView as:

    View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false);
    TextView  mQuestionText = (TextView)rootView.findViewById(R.id.questionTextView);