0

I have some code in an Android app which was working fine but is now throwing an NPE when trying to locate a LinearLayout. The first line of the ReadRecords method below is returning a null for the linearLayoutRecords object, which is causing an NPE on the 2nd line:

public class SavedMealsActivity extends Activity {

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

        // enable the home button
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);

        // Read saved meal records from the database and display them
        readRecords();

    }

    public void readRecords() {
        LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
        linearLayoutRecords.removeAllViews();

The layout XML is here:

<LinearLayout 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:background="@drawable/background"
    android:orientation="vertical"
    tools:context="com.ian.mealtimer.SavedMealsActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/activity_saved_meals_textview1"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ScrollView
        android:id="@+id/scrollViewRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewRecordCount"
        android:layout_margin="5dip"
        android:padding="5dip" >

        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Any ideas??

Ian M
  • 567
  • 8
  • 33
  • 1
    Wild guess: the LinearLayout is on the Fragment layout xml, not on the Activity layout xml. – Phantômaxx Aug 01 '14 at 17:57
  • No, the LinearLayout is in the Activity layout XML – Ian M Aug 01 '14 at 17:59
  • (1) Is the layout file that you have posted named correctly (activity_saved_meals.xml)? (2) Can you post the stack trace of the exception? – rgamber Aug 01 '14 at 18:05
  • Yes the layout file is named correctly. The LinearLayout is found by the IDE at design time (if I type "findViewById(R.id." in Eclipse it pops up the linearLayoutRecords name). – Ian M Aug 01 '14 at 18:16

0 Answers0