Android 试图查找LinearLayout的NullPointerException
我在一个Android应用程序中有一些代码,它工作得很好,但现在在尝试定位线性布局时抛出了一个NPE。下面ReadRecords方法的第一行为linearLayoutRecords对象返回null,这导致第二行出现NPE:Android 试图查找LinearLayout的NullPointerException,android,nullpointerexception,android-linearlayout,Android,Nullpointerexception,Android Linearlayout,我在一个Android应用程序中有一些代码,它工作得很好,但现在在尝试定位线性布局时抛出了一个NPE。下面ReadRecords方法的第一行为linearLayoutRecords对象返回null,这导致第二行出现NPE: public class SavedMealsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.o
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>
有什么想法吗?胡乱猜测:LinearLayout在片段布局xml上,而不是在活动布局xml上。不,LinearLayout在活动布局XML1中,您发布的布局文件名为Activity\u saved\u fines.xml吗?2能否发布异常的堆栈跟踪?是,布局文件的名称正确。如果我键入findViewByIdR.id,则IDE会在设计时找到LinearLayout。在Eclipse中,它会弹出linearLayoutRecords名称。