为Main GridLayout调用findviewbyid将返回null

时间:2017-11-22 21:32:38

标签: java android android-layout android-gridlayout

我正在尝试为findviewbyid调用GridLayout,但无论何时放置它都会返回null。在设置onCreate()并膨胀ViewStub后,我将其称为contentView方法。除content_pytanie1.xml以外,mQuestionLayout的所有孩子都会正常退回。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pytanie1);
    ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
    stub.setLayoutResource(R.layout.content_pytanie1);
    stub.inflate();
     mQuestionView = (TextView) findViewById(R.id.question);
     mImageName = (ImageView) findViewById(R.id.iV_pytanie);
     mButtonChoice1 = (RadioButton) findViewById(R.id.choice1);
     mButtonChoice2 = (RadioButton) findViewById(R.id.choice2);
        ....
    mQuestionLayout = (GridLayout) findViewById(R.id.gridLayout3); //returns null
}

GridLayout位于content_pytanie1.xml布局中。无论我如何为RelativeLayout更改它。

    <?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
    tools:context="pl.epsotesty.epsotestynumeryczne.pytanie1Activity"
    android:orientation="vertical"
    android:animateLayoutChanges="true"
    android:id="@+id/gridLayout3"
    >
            <ImageView
                android:id="@+id/iV_pytanie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="fitStart"
                android:adjustViewBounds="true"
                />
    <TextView
                android:id="@+id/tV_p"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tV_p"
                android:textAppearance="@style/QuestionsAnswers"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="5dp"
                />
            <TextView
                android:id="@+id/question"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Question"
                android:textAppearance="@style/QuestionsAnswers"
                android:layout_marginBottom="4dp"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="5dp"
                 />
            <RadioGroup
                android:id="@+id/radioGroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="12dp"
                android:orientation="horizontal"
                android:gravity="bottom"
                android:layout_gravity="center_horizontal"
                android:layout_marginStart="5dp"
                android:layout_marginEnd="5dp"
                >
                <RadioButton
                    android:id="@+id/choice1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="13dp"
                    android:layout_marginRight="13dp"
                    android:layout_weight="1"
                    android:button="@null"
                    android:checked="false"
                    android:drawableBottom="@android:drawable/btn_radio"
                    android:drawablePadding="2dp"
                    android:gravity="center|bottom"
                    android:layout_gravity="bottom"
                    android:text="fkdl;avnvbnsdk"
                    android:textAppearance="@style/QuestionsAnswers" />
                <RadioButton
                    android:id="@+id/choice2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="13dp"
                    android:layout_marginRight="13dp"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="@android:drawable/btn_radio"
                    android:gravity="center|bottom"
                    android:text="dsakdlvnvbnvnkd"
                    android:layout_gravity="bottom"
                    android:textAppearance="@style/QuestionsAnswers"
                    android:drawablePadding="2dp"
                    />
                <RadioButton
                    android:id="@+id/choice3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Cdsadvnbvbndas"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="@android:drawable/btn_radio"
                    android:textAppearance="@style/QuestionsAnswers"
                    android:layout_marginEnd="13dp"
                    android:gravity="center|bottom"
                    android:layout_gravity="bottom"
                    android:layout_marginRight="13dp"
                    android:drawablePadding="2dp"
                    />
                <RadioButton
                    android:id="@+id/choice4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ddsadbvnva"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="@android:drawable/btn_radio"
                    android:gravity="center|bottom"
                    android:textAppearance="@style/QuestionsAnswers"
                    android:layout_marginEnd="13dp"
                    android:layout_gravity="bottom"
                    android:layout_marginRight="13dp"
                    android:drawablePadding="2dp"
                    />
                <RadioButton
                    android:id="@+id/choice5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Envnvnvnnvnvbnnnmbmbnmb"
                    android:layout_weight="1"
                    android:button="@null"
                    android:drawableBottom="@android:drawable/btn_radio"
                    android:gravity="center|bottom"
                    android:textAppearance="@style/QuestionsAnswers"
                    android:layout_gravity="bottom"
                    android:layout_marginEnd="13dp"
                    android:drawablePadding="2dp"
                    android:layout_marginRight="13dp" />
            </RadioGroup>
    <TextView
        android:id="@+id/wyjasnienie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:text="Wyjaśnienie:"
        android:textAppearance="@style/QuestionsAnswers"
        />
    <TextView
        android:id="@+id/tekstwyjasnienia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:text="tekst wyjaśnienia"
        android:textAppearance="@style/QuestionsAnswers"
        />
    <TextView
        android:id="@+id/zaznaczenie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="zaznaczono"
        android:textAppearance="@style/QuestionsAnswers" />
    <TextView
        android:id="@+id/lista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="lista"
        android:textAppearance="@style/QuestionsAnswers" />
</GridLayout>

这是activity_pytanie1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="match_parent"
    android:layout_height="match_parent"
    tools:context="pl.epsotesty.epsotestynumeryczne.pytanie1Activity"
    >
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="36dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tVtitle"
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="title"
                android:textAlignment="textStart"
                android:textSize="15sp"
                android:paddingLeft="14dp"
                 />
            <TextView
                android:id="@+id/tVcT"
                android:layout_width="match_parent"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="600"
                 />
                <Button
                    android:id="@+id/buttonkoniec"
                    android:layout_width="26dp"
                    android:layout_height="26dp"
                    android:layout_marginEnd="20dp"
                    android:layout_gravity="center"
                    android:background="@drawable/exit"
                    android:onClick="onClickEndTest"
                    />
        </LinearLayout>
        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/CustomProgressBarHorizontal4"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:progress="100"/>
    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appBar2"
        android:layout_above="@id/toolbar"
        >
        <ViewStub
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inflatedId="@+id/message_layout"
            android:id="@+id/layout_stub" />
    </ScrollView>

2 个答案:

答案 0 :(得分:0)

您可以在xml中添加ViewStub布局:

<ViewStub
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inflatedId="@+id/message_layout"
    android:layout="@layout/content_pytanie1"
    android:id="@+id/layout_stub" />

然后:

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.inflate();

或以编程方式:

ViewStub viewStub = (ViewStub) findViewById(R.id.layout_stub);
viewStub.setLayoutResource(R.layout.content_pytanie1);
viewStub.inflate();
  

说明:

该行

android:inflatedId="@+id/message_layout"

表示虚增布局的ID为message_layout,但您的GridLayout标识为gridLayout3,这就是null的原因。

所以要么删除这一行:

android:inflatedId="@+id/message_layout"

然后执行:

mQuestionLayout = (GridLayout) findViewById(R.id.gridLayout3);

或只是这样做:

mQuestionLayout = (GridLayout) findViewById(R.id.message_layout);

答案 1 :(得分:0)

要从View访问ViewStub,请执行以下操作:

View view = stub.inflate(); //view is an inflated View
mQuestionLayout = (GridLayout) view.findViewById(R.id.gridLayout3);

这一行:

stub.inflate();

return一个View,您将用它来访问GridLayout