mdtsandman
mdtsandman

Reputation: 546

Trouble with findViewById() ... returns NULL when I don't think it should

This is an Android resource file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
android:paddingLeft="20dip"
android:paddingRight="20dip" android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:id="@+id/pt_name_view"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
    android:id="@+id/pt_dob_view"/>

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
    android:id="@+id/pt_mrun_view"/>

</LinearLayout> 

This is the corresponding class within the Android application:

public class PatientViewActivity extends Activity {

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

        // ... some irrelevant code ... 

        final TextView nameView = (TextView) findViewById(R.id.pt_name_view);
        nameView.setText(pt.getSurname().toUpperCase() + ", " + pt.getGivenNames() );

        // ... some more irrelevant code ...

}

This code worked fine until recently. Now suddenly nameView is NULL when the code is run despite the fact that neither file has been altered.

Upvotes: 0

Views: 229

Answers (1)

Pete Houston
Pete Houston

Reputation: 15079

Whenever you make a little change to resources (even add a space, a character, moving file, add files...), your project compilation might go rogue sometimes.

Just keep in mind, nothing is perfect and have a good practice. You've learned a lesson: always save & clean project before running.

Upvotes: 1

Related Questions