문제

I used a Merge -Tag for a my class extend Fragement. But I met error when I was trying to access elements of R class.

My logcat shows: "android.view.InflateException: can be used only with a valid ViewGroup root and attachToRoot=true" . Bellow is my source code. Please help me. I sorry about my english, it is not good.

public class DetailHeader extends Fragment {
ImageView imageView;
TextView textHeader;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     LinearLayout wrapper = new LinearLayout(getActivity()); // for example
     inflater.inflate(R.layout.detail_header, wrapper, true);
     View view = inflater.inflate(R.layout.detail_header, null);
     imageView =(ImageView)view.findViewById(R.id.image);
     textHeader =(TextView)view.findViewById(R.id.textViewHeader);
     imageView.setImageResource(R.drawable.dengiong);
     return wrapper;
     }
}

This is my xml file:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textViewHeader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginBottom="20dip"
    android:padding="12dip"
    android:text="Golden Gate"
    android:textColor="#ffffffff" />

</merge>
도움이 되었습니까?

해결책

If you use the <merge> tag as your root view in a layout, that layout can only be used via <include> from another layout. Otherwise your inflated layout has no root ViewGroup in which to hold the child views (in your case the ImageView and TextView.) So you need to replace the <merge> with a real ViewGroup instance, such as LinearLayout or FrameLayout.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top