【问题标题】:Get LinearLayout reference at custom view在自定义视图中获取 LinearLayout 参考
【发布时间】:2021-10-21 14:36:54
【问题描述】:

我的问题是关于 Android/Java。

我想从我的自定义视图中访问其他视图。

我的 main.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLinearLayout1">

    <org.javaforum.input
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        required=""
        android:hint="Enter your E-Mail"
        oninvalid="this.setCustomValidity('SO - Please enter a correct E-Mail!!!')"/>

    <org.javaforum.input
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        inputType="submit"/>

</LinearLayout>

所以当点击第二个“输入”视图时,我想检查第一个“输入”视图中是否存在名为“必需”的属性。

我在 input.java 中试过这个:

public class input extends TextView{
    public input(Context context, AttributeSet attr) {
        super(context, attr, getIdentifier(context,attr));
    }

    @Override
    public void onFinishInflate() {
        setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout layout = (LinearLayout)findViewById(R.id.mainLinearLayout1);
                View v = null;
                for(int i=0; i<layout.getChildCount(); i++) {//Error
                    //Code
                }
            }
        });
    }
}

但我收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.LinearLayout.getChildCount()' on a null object reference

我不明白为什么它是空的。应该已经创建了吧?

那么我怎样才能在我的自定义视图中正确获取我的 main.xml 的 LinearLayout 引用?

【问题讨论】:

    标签: java android android-layout android-linearlayout android-custom-view


    【解决方案1】:

    要解决空指针异常,您需要在父 LinearLayout 中搜索其他小部件。您可以使用View.getParent() 来识别父级。由于您要查找的视图是当前视图的同级视图,因此您将在父视图中找到它。

    至于确定是否设置了某个属性,我建议通过其视图捕获该值,并提供一种方法来根据请求呈现该值。假设该值是您的“必需”值,然后在本地捕获该值并提供一个方法,例如 getRequiredValue() 来公开它。

    【讨论】:

    • 提供方法和提供变量来获取“需要”的值是一样的吗?
    • @Button 由于“必需”值是您定义的,因此是自定义属性,您需要做一些工作来定义和从 XML 检索它的值。见Define Custom Attributes
    • 我从 AttributeSet 对象中提取了“必需”属性。但我有一个问题。也许我需要就这个问题提出另一个问题......
    【解决方案2】:

    你可以使用

     LinearLayout layout = (LinearLayout) getRootView().findViewById(R.id.mainLinearLayout1);
    

    如果您只在自定义视图中调用findViewById,它只会查找子视图。

    【讨论】:

    • 哈哈,难以置信,一点点内联修改可以改变整个应用程序...
    • 但我应该建议你看看@Cheticamp 解决方案,它只找到父视图而不是找到根视图并再次找到父视图。如果您想找到另一个 id 而不是父视图,我的解决方案会更好。
    【解决方案3】:

    这应该在 Activity 的 onCreate 而不是 TextView 的 onFinishInflate 中调用。

    首先给提交按钮一个id,例如btn_submit,然后在onCreate中

        Button btn_submit = (Button)findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LinearLayout layout = (LinearLayout)findViewById(R.id.mainLinearLayout1);
                View v = null;
                for(int i=0; i<layout.getChildCount(); i++) {
                    //Code
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2021-02-09
      相关资源
      最近更新 更多