这是我被卡住的片段。我想要识别layout_root
fragment_new_weather.xml的片段
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingStart="@dimen/activity_horizontal_margin"
android:paddingEnd="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.a5corp.weather.WeatherActivity$PlaceholderFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/city_field"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:ellipsize="end"
android:maxLines="1"
android:textSize="48sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/linearLayout2"
android:layout_below="@+id/daily_view"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:id="@+id/temp_text_view"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:id="@+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:id="@+id/temp_text_view1"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:minWidth="85sp"
android:paddingEnd="2dp"
android:layout_marginEnd="8dp"
android:paddingLeft="5dp"
android:paddingRight="2dp"
android:layout_marginRight="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/weather_icon1"
android:textSize="70sp"
android:textColor="#FFFFFF"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/details_view1"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- Code below this in the file works properly-->
这是我的片段的onCreateView我坚持:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Working Code above
layout = (LinearLayout) getActivity().findViewById(R.id.layout_root);
if (layout == null)
Log.e("NewFragment" , "Layout is null");
if (rootView.findViewById(R.id.temp_text_view) == null)
Log.e("NewFragment" , "Temp Text Is Null");
if (rootView.findViewById(R.id.temp_text_view1) == null)
Log.e("NewFragment" , "Temp Text 1 Is Null");
//Working code below
}
问题是布局总是返回null,而temp_text_view(layout_root外部)和temp_text_view1(layout_root内部)都不返回 null,而layout_root总是返回null。
以下是我正在尝试实施的代码:
for (int i = 0; i < 10; ++i) {
LinearLayout child = new LinearLayout(getContext());
child.setOrientation(LinearLayout.VERTICAL);
child.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.MATCH_PARENT));
child.setPadding(5 , 0 , 2 , 0);
child.setPaddingRelative(5 , 0 , 2 , 0);
TextView wIcon = new TextView(getContext());
TextView detField = new TextView(getContext());
wIcon.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
wIcon.setTextSize(70);
wIcon.setTextColor(Color.WHITE);
wIcon.setTextAppearance(getContext() , android.R.style.TextAppearance_Large);
wIcon.setGravity(Gravity.CENTER);
detField.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
detField.setTextAppearance(getContext() , android.R.style.TextAppearance_Medium);
detField.setTextColor(Color.WHITE);
wIcon = weatherIcon[i];
detField = detailsField[i];
layout.addView(wIcon);
layout.addView(detField);
}
答案 0 :(得分:2)
更改,
layout = (LinearLayout) getActivity().findViewById(R.id.layout_root);
要,
layout = (LinearLayout) rootView.findViewById(R.id.layout_root);
当您尝试访问getActivity()
中的某个元素时,请使用Activity
。当您尝试访问当前布局中的某些内容时使用rootView,在本例中为fragment_new_weather.xml
。