尝试查找LinearLayout的NullPointerException

时间:2014-08-01 17:56:22

标签: android nullpointerexception android-linearlayout

我在Android应用程序中有一些代码正常工作但现在在尝试查找LinearLayout时抛出NPE。下面的ReadRecords方法的第一行是为linearLayoutRecords对象返回一个null,这会导致第二行的NPE:

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();

布局XML在这里:

<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>

任何想法??

0 个答案:

没有答案