程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了尝试在 TextView 中显示 EditText 值时的空对象引用 - Android大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决尝试在 TextView 中显示 EditText 值时的空对象引用 - Android?

开发过程中遇到尝试在 TextView 中显示 EditText 值时的空对象引用 - Android的问题如何解决?下面主要结合日常开发的经验,给出你关于尝试在 TextView 中显示 EditText 值时的空对象引用 - Android的解决方法建议,希望对你解决尝试在 TextView 中显示 EditText 值时的空对象引用 - Android有所启发或帮助;

我知道这个问题已经被问过几次了,但是我已经尝试了解决方案并且错误仍然出现在调试器中。

我有一个小表格,上面有宠物的名字、宠物的种类和品种。我有两个微调器,其中第二个基于第一个值,然后我有一个开关盒,当用户按下 go 时,它会将用户带到正确的品种页面。

基本上我想在我的开关盒中添加一个 setText,它会说“现在显示宠物名称的定制信息”,但是当我在调试器上运行时,它会抛出一个null object reference error

我有几个具有相同 ID 的 textVIEws,因此我将其更改为唯一的 ID,但没有任何区别。感谢任何帮助。

错误

E/AndroIDRuntime: FATAL EXCEPTION: main
    Process: com.example.petsopedia,PID: 15588
    java.lang.NullPointerException: Attempt to invoke virtual method 'voID androID.Widget.TextVIEw.setText(java.lang.CharSequencE)' on a null object reference
        at com.example.petsopedia.Questionnaire$3$1.onClick(Questionnaire.java:117)
        at androID.vIEw.VIEw.performClick(VIEw.java:5610)
        at com.Google.androID.material.button.Materialbutton.performClick(Materialbutton.java:992)
        at androID.vIEw.VIEw$PerformClick.run(VIEw.java:22265)
        at androID.os.Handler.handleCallBACk(Handler.java:751)
        at androID.os.Handler.dispatchmessage(Handler.java:95)
        at androID.os.Looper.loop(Looper.java:154)
        at androID.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:756)

XML

<EditText
    androID:ID="@+ID/petname"
    androID:layout_wIDth="@dimen/textfIEld_wIDth"
    androID:layout_height="wrap_content"
    androID:autofillHints="@String/pet_name"
    androID:ems="10"
    androID:FontFamily="serif"
    androID:hint="@String/pet_name"
    androID:inputType="textPersonname"
    androID:labelFor="@String/pet_name"
    app:layout_consTraintBottom_toBottomOf="parent"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintHorizontal_bias="0.495"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_totopOf="parent"
    app:layout_consTraintVertical_bias="0.65" />

<Spinner
    androID:ID="@+ID/breed_spinner"
    androID:layout_wIDth="@dimen/textfIEld_wIDth"
    androID:layout_height="@dimen/spinner_height"
    androID:FontFamily="serif"
    androID:labelFor="@ID/breed_spinner"
    androID:text="@String/peT_Breed"
    app:layout_consTraintBottom_toBottomOf="parent"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintHorizontal_bias="0.495"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_totopOf="parent"
    app:layout_consTraintVertical_bias="0.804" />

<Spinner
    androID:ID="@+ID/which_spinner"
    androID:layout_wIDth="@dimen/textfIEld_wIDth"
    androID:layout_height="@dimen/spinner_height"
    androID:FontFamily="serif"
    androID:labelFor="@ID/which_spinner"
    androID:text="@String/pet_type"
    app:layout_consTraintBottom_toBottomOf="parent"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintHorizontal_bias="0.495"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_totopOf="parent"
    app:layout_consTraintVertical_bias="0.735" />

<linearLayout
    androID:ID="@+ID/linearLayout"
    androID:layout_wIDth="216dp"
    androID:layout_height="wrap_content"
    androID:orIEntation="vertical"
    app:layout_consTraintBottom_toBottomOf="parent"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintHorizontal_bias="0.497"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_totopOf="parent"
    app:layout_consTraintVertical_bias="0.9">

    <RadioGroup
        androID:ID="@+ID/radioGroup"
        androID:layout_wIDth="match_parent"
        androID:layout_height="40dp"
        androID:orIEntation="horizontal">

        <Radiobutton
            androID:ID="@+ID/maleRadio"
            androID:layout_wIDth="108dp"
            androID:layout_height="match_parent"
            androID:FontFamily="serif"
            androID:text="@String/male" />

        <Radiobutton
            androID:ID="@+ID/femaleRadio"
            androID:layout_wIDth="108dp"
            androID:FontFamily="serif"
            androID:layout_height="match_parent"
            androID:text="@String/female" />

    </RadioGroup>

</linearLayout>

<button
    androID:ID="@+ID/goQuestionnaire"
    androID:layout_wIDth="wrap_content"
    androID:layout_height="wrap_content"
    androID:BACkground="@drawable/buttons"
    androID:text="@String/go_button"
    app:layout_consTraintBottom_toBottomOf="parent"
    app:layout_consTraintEnd_toEndOf="parent"
    app:layout_consTraintHorizontal_bias="0.498"
    app:layout_consTraintStart_toStartOf="parent"
    app:layout_consTrainttop_totopOf="parent"
    app:layout_consTraintVertical_bias="0.976" />

目标页面上的TextVIEw

    <TextVIEw
        androID:ID="@+ID/txResultAlsatian"
        androID:layout_wIDth="match_parent"
        androID:layout_height="0dp"
        androID:layout_weight="1" />

Java

       EditText petname  = (EditText) findVIEwByID(R.ID.petName);
        TextVIEw alsatianResult = (TextVIEw) findVIEwByID(R.ID.txResultAlsatian);

     breedSpinner.setonItemSELEctedListener(new AdapterVIEw.onItemSELEctedListener()
        {
            @OverrIDe
            public voID onItemSELEcted(AdapterVIEw<?> parent,View view,int position,long ID)
            {

                switch (breedSpinner.getSELEctedItem().toString()) {
                    case "Alsatian":
                        gobutton.setonClickListener(new VIEw.onClickListener() {

                            public voID onClick(VIEw v)
                            {
                                String value = petname.getText().toString();
                                //result.setText("name:" + txValue );
                                alsatianResult.setText(value);
                                Intent intent = new Intent(Questionnaire.this,PeTinfoAlsatian.class);

                                startActivity(intent);



                            }
                        });
                        break;
         }
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的尝试在 TextView 中显示 EditText 值时的空对象引用 - Android全部内容,希望文章能够帮你解决尝试在 TextView 中显示 EditText 值时的空对象引用 - Android所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。