如何将视图添加到片段中的 LinearLayout?

问题描述 投票:0回答:0

我正在尝试将视图添加到我的 xml 中定义的布局,但它抛出一个空引用 它是应用程序中定义的片段

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_manager, container, false);

    LinearLayout layout = rootView.findViewById(R.id.buttons);
    TextView test = new TextView(getContext());
    test.setText("Test");
    layout.addView(test);

    return rootView;
}

框架布局

<FrameLayout 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/fondo"
tools:context=".ui.planets.ButtonsFragment">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/app_name" >

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

</ScrollView>
android layout view fragment add
© www.soinside.com 2019 - 2024. All rights reserved.