访问Android中任何视图的父视图

时间:2018-09-02 15:59:18

标签: android

我的xml代码中的TextInputEditText中有一个TextInputLayout

<android.support.design.widget.TextInputLayout
        android:id="@+id/textfieldContainer2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/customErrorTextColor"
        app:hintTextAppearance="@style/customHintTextAppearance"
        app:layout_constraintTop_toBottomOf="@id/textfieldContainer">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/bottomtextfield"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="add bottom text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />

</android.support.design.widget.TextInputLayout>

问题

我正尝试通过TextInputLayout获得TextInputEditText,如下所示

TextInputLayout parentInputLayout = (TextInputLayout) textfield.getParent();

但是这一行代码会抛出java.lang.IllegalStateException

问题

我在这里做错了什么,如何通过TextInputLayout的引用获得任何TextInputEditText的父母TextInputEditText

Stacktrace

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.yousafkhan.memegeneratorapp, PID: 10425
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
                      at android.view.View.performClick(View.java:6597)
                      at android.view.View.performClickInternal(View.java:6574)
                      at android.view.View.access$3100(View.java:778)
                      at android.view.View$PerformClick.run(View.java:25885)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6669)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
                      at android.view.View.performClick(View.java:6597) 
                      at android.view.View.performClickInternal(View.java:6574) 
                      at android.view.View.access$3100(View.java:778) 
                      at android.view.View$PerformClick.run(View.java:25885) 
                      at android.os.Handler.handleCallback(Handler.java:873) 
                      at android.os.Handler.dispatchMessage(Handler.java:99) 
                      at android.os.Looper.loop(Looper.java:193) 
                      at android.app.ActivityThread.main(ActivityThread.java:6669) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
                   Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.design.widget.TextInputLayout
                      at com.example.yousafkhan.memegeneratorapp.MainActivity.textFieldEmpty(MainActivity.java:64)
                      at com.example.yousafkhan.memegeneratorapp.MainActivity.createMeme(MainActivity.java:53)

2 个答案:

答案 0 :(得分:1)

可以通过以下步骤获取对包含TextInputLayout的{​​{1}}的引用:

TextInputEditText

这是因为TextInputEditText textfield = findViewById(R.id.bottomtextfield); ViewGroup containingLayout = (ViewGroup) textfield.getParent(); TextInputLayout textInputLayout = (TextInputLayout) containingLayout.getParent(); 上有一个TextInputLayout,其中包含关联的FrameLayout

答案 1 :(得分:0)

xml中的视图层次结构可能与android返回的视图层次结构不同。因此getParent()可能不会返回您期望的视图。

因此,您最好使用:

TextInputLayout parentInputLayout = findViewById(R.id.textfieldContainer2);