Search code examples
androidtextviewoncreatesettext

TextView.setText() location confusion: onCreate() vs userMethod()


I'm curious about where setText() actually works. Here is a sample code for my question. I skipped irrelevant code with "..." ellipse.

// fragment_main.xml
<RelativeLayout ... >

    <TextView
    android:id="@+id/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="userMethod" />

</RelativeLayout>

and

// ActivityMain.java
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.hello_world);
    textView.setText("This cause runtime error!!");

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    } // if
} // onCreate

    /**
 * 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) {

                         // gives a compile error about non-static finViewById here
                 // TextView textView = (TextView) findViewById(R.id.hello_world);
                 // textView.setText("Compiling Error!!");


        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    } // onCreateView
} // PlaceholderFragment


// user defined method 
public void userMethod(View view) {
    TextView textView = (TextView) findViewById(R.id.hello_world);
    textView.setText("This run successful");
} // userMethod
} // class MainActivity

Here is what I don't understand:

textView.setText("string") fail when in protected void onCreate(...)

but

textView.setText("string") run when in public void userMethod(...)

yet

both these methods are in public class MainActivity extends ActionBarActivity

What is the difference??


Solution

  • TextView apparently is in fragment layout. You are trying to reach the textview before you inflate the fragment that's why you get null pointer exception.

    Try to settext inside fragment onCreateView() method or after fragment inflate.

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // we inflate the fragment here.
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
           // get the textview from inflated layout.
           TextView textView = (TextView) rootView.findViewById(R.id.hello_world);
           textView.setText("Should work");
    
           return rootView;
        }