gpt4 book ai didi

java - 为主 GridLayout 调用 findviewbyid 返回 null

转载 作者:行者123 更新时间:2023-12-02 11:58:17 26 4
gpt4 key购买 nike

我正在尝试为 GridLayout 调用 findviewbyid,但无论我放置在哪里,它都会返回 null。在设置 contentView 并膨胀 ViewStub 后,我将其称为 onCreate() 方法。除了 mQuestionLayout 之外,content_pytanie1.xml 中的所有子项均正确返回。

    @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>

最佳答案

您可以在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();

Explanation:

线路

android:inflatedId="@+id/message_layout"

意味着膨胀布局的id将为message_layout,但你的GridLayout有idgridLayout3,这就是为什么它是null .

因此,要么删除该行:

android:inflatedId="@+id/message_layout"

然后执行:

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

或者只是这样做:

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

关于java - 为主 GridLayout 调用 findviewbyid 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47444172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com