每当我运行此代码并转到我的课程片段(下面的代码)时,我会得到一个致命的异常'java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.view.View。 findViewById(int)'对空对象引用'。这必须与片段有关,就像我在单个活动中运行此代码一样,它工作正常。当我引用RelativeLayout并尝试findViewById(R.id.rl)时会出现错误,但是android studio本身确实知道它存在,因为它突出显示为紫色。
LessonFragment.class:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* Set the layout to use the fragment_lesson file */
View rootView = inflater.inflate(R.layout.fragment_lesson, container, false);
//Find and initalise the buttons on the page
nextBtn = (ImageButton) rootView.findViewById(R.id.NextButton);
previousBtn = (ImageButton) rootView.findViewById(R.id.PreviousButton);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Simply change the lessonNumber to the next lesson, or to the first lesson if the user is on the last one.
//This creates a loop effect so the user can not run over the lessons.
//------ THIS NEEDS TO CHANGE SO THAT THEY GET THE PUBLIC LESSON NUMBER (PERHAPS FROM USER CLASS)
if (lessonNumber == 10) {
lessonNumber = 0;
}
nextLesson();
createPage(lessonNumber);
}
});
previousBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Simply change the lessonNumber to the previous lesson, or to the last lesson if the user is on the first one.
//This creates a loop effect so the user can not run over the lessons.
//------ THIS NEEDS TO CHANGE SO THAT THEY GET THE PUBLIC LESSON NUMBER (PERHAPS FROM USER CLASS)
if (lessonNumber == 1) {
lessonNumber = 10;
}
previousLesson();
createPage(lessonNumber);
}
});
//Find and reference the layouts that we will be adding our text to
RelativeLayout rl=(RelativeLayout) getView().findViewById(R.id.rl);
ScrollView sv = (ScrollView)getView().findViewById(R.id.sl);
LinearLayout ll = (LinearLayout)getView().findViewById(R.id.ll);
createPage(1);
return rootView;
}
lesson_xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/nav_header_height"
android:paddingBottom="@dimen/activity_vertical_margin" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sl"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/PreviousButton" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/HTMLTitle"
android:id="@+id/LessonTitle"
android:textColor="#0d9445"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Lorem"
android:id="@+id/p1"
android:layout_marginTop="74dp" />
</LinearLayout>
</ScrollView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PreviousButton"
android:src="@drawable/previous"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:theme="@android:style/Holo.ButtonBar"
android:contentDescription="@string/buttonName" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/NextButton"
android:src="@drawable/next"
android:theme="@android:style/Holo.ButtonBar"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:contentDescription="@string/buttonName2" />
</RelativeLayout>