I am trying to add various fragments to an Action bar Activity.
我正在尝试将各种片段添加到操作栏活动中。
Here is one of the fragments (nested inside the main activity):
这是一个片段(嵌套在主要活动中):
public static class ScheduleFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static ScheduleFragment newInstance(int sectionNumber) {
ScheduleFragment fragment = new ScheduleFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ScheduleFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.schedule_layout, container, false);
return rootView;
}
}
And the respective xml:
和各自的xml:
<RelativeLayout
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/textView"/>
</RelativeLayout>
And when I call
当我打电话的时候
titles.add((TextView) findViewById(R.id.textView));
titles.get(0).setText(R.string.Departure_Location);
from within the MainActivity class, I get Null pointer exception, and I'm guessing that's because R.id.textView is defined in the "schedule_layout.xml" and not in the "main_activity.xml".
从MainActivity类中,我得到Null指针异常,我猜这是因为R.id.textView是在“schedule_layout.xml”中定义的,而不是在“main_activity.xml”中定义的。
Android studio checker finds the R.id.textView, even though I have no textView in the "main_activity.xml".
Android studio checker找到R.id.textView,即使我在“main_activity.xml”中没有textView。
So how exactly can I sort this out?
那么我怎么能解决这个问题呢?
1 个解决方案
#1
0
The answer to this is quite straightforward:
答案很简单:
Since I do
自从我这样做
View rootView = inflater.inflate(R.layout.schedule_layout, container, false);
I add the "schedule_layout" to the rootView, so basically, when I do
我将“schedule_layout”添加到rootView,基本上,当我这样做时
titles.add((TextView) findViewById(R.id.textView));
I tell the program to grab the textView from the main_activity layout, and since there is none, it returns null.
我告诉程序从main_activity布局中获取textView,因为没有,它返回null。
So I need to tell it to get it from schedule_layout, by:
所以我需要告诉它从schedule_layout获取它,通过:
titles.add((TextView) rootView.findViewById(R.id.textView));
#1
0
The answer to this is quite straightforward:
答案很简单:
Since I do
自从我这样做
View rootView = inflater.inflate(R.layout.schedule_layout, container, false);
I add the "schedule_layout" to the rootView, so basically, when I do
我将“schedule_layout”添加到rootView,基本上,当我这样做时
titles.add((TextView) findViewById(R.id.textView));
I tell the program to grab the textView from the main_activity layout, and since there is none, it returns null.
我告诉程序从main_activity布局中获取textView,因为没有,它返回null。
So I need to tell it to get it from schedule_layout, by:
所以我需要告诉它从schedule_layout获取它,通过:
titles.add((TextView) rootView.findViewById(R.id.textView));