【问题标题】:getting all dynamically created linearlayout android java获取所有动态创建的线性布局android java
【发布时间】:2023-03-31 09:30:01
【问题描述】:
ScrollView sv = (ScrollView) v.findViewById(R.id.layout_question_content);
LinearLayout layout_questions = (LinearLayout) sv.findViewWithTag("layout_questions");
int childcount = layout_questions.getChildCount();
System.out.println("LinearLayout " + childcount);

我想使用标签获取所有线性布局,但我得到了

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.View android.widget.ScrollView.findViewWithTag(java.lang.Object)'

我的所有 LinearLayout 包括它的所有子元素都是动态添加的

XML

<ScrollView
        android:id="@+id/layout_question_content"
        android:clickable="true"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_marginBottom="50px"
        android:layout_height="fill_parent">

</ScrollView>

【问题讨论】:

  • 您的svnull。愿意分享您的 XML?
  • 您确定您的 Scrollview ID 正确吗?
  • 你在哪里写这个 sn-p 里面 onCreate() ?或者别的地方 ? ScrollView 未初始化。它将抛出一个空对象引用。分享你写这个的方法,也分享xml。
  • 我有另一个声明 ScrollView sv = (ScrollView) v.findViewById(R.id.layout_question_content); 这个声明我得到一个错误。所以我所做的就是再次声明它。这是我将视图添加到的滚动视图
  • 再次,请使用您正在使用的 XML 文件更新您的答案。没有它,这里的每个人都只能玩猜谜游戏,而不能给出实际可行的答案。

标签: java android android-layout android-linearlayout


【解决方案1】:

ScrollView 要么未在您的 XML 中定义,要么您的 &lt;ScrollView /&gt; 没有 ID layout_question_content。检查您是否设置了android:id="@+id/layout_question_content"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多