1

在最初只有一个标题和一个“+”按钮的活动中,我想在每次单击按钮时添加包含更多视图的线性布局。

我不知道如何解决这个问题,所以我尝试了以下操作:最多有七个 LinearLayouts 要添加,所以我将它们添加到 layout-xml-file 中并将它们全部设置为android:visibility="gone"

这是我的布局 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp" >

    <LinearLayout 
        android:id="@+id/layout_ppe2" 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        >

         (...)

        <Button 
        android:id="@+id/bt_ppe2_newset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickAddNewSet"
        android:text="@string/plus"
        />

        (...)

        <LinearLayout 
            android:id="@+id/lin_ppe2_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal"
            android:visibility="gone">
            (...)
        </LinearLayout>

        <LinearLayout 
            android:id="@+id/lin_ppe2_3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:orientation="horizontal"
            android:visibility="gone">
            (...)
        </LinearLayout>

        (...)

    </LinearLayout>


</ScrollView>

有LinearLayouts lin_ppe2_2 - lin_ppe2_8,这是七个通过单击按钮一个接一个设置可见的布局。

现在onClickAddNewSet我得到了以下(int counter初始化=1):

public void onClickAddNewSet(View v){
        ScrollView scrollView = (ScrollView) getLayoutInflater().inflate(R.layout.all_completed_bets, null);
        LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.layout_ppe2);
        LinearLayout layout2 = null;
        switch(counter){
        case 1:
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_2);
            break;
        case 2:
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_3);
            break;
        case 3:
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_4);
            break;
        case 4: 
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_5);
            break;
        case 5:
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_6);
            break;
        case 6:
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_7);
            break;
        case 7: 
            layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_8);
            break;
        }
        if(counter>0 && counter <8){
            layout2.setVisibility(LinearLayout.VISIBLE);
        }
        counter++;  
    }

这总是给我提到的错误与 a NullPointerExceptionat the line layout2.setVisibility(LinearLayout.VISIBLE);。我也试过

LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.layout_ppe2);
LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.lin_ppe2_2);
layout2.setVisibility(LinearLayout.VISIBLE);

检查是否发生错误,因为 layout2 是用 null 初始化的,但计数器工作正常(我用setText(counter)活动标题-TextView 对其进行了测试)。所以现在我真的不知道为什么这让我这个NullPointerException。我是否忽略了一些非常明显的东西?还是有更好的方法来实现我的目标?注意:我不仅要添加这个LinearLayouts,稍后我还必须读出一些值(每个LinearLayout中有2个EditText)。

4

2 回答 2

-1

尝试更改 <android:visibility="gone"><android:visibility="invisible">

我相信该gone属性会强制它在布局期间被完全忽略,您希望它被布局但不可见。

于 2012-09-25T23:28:07.337 回答
-1

而不是膨胀一个新的 Scrollview 实例,只需操纵附加到您的活动的实际 Scrollview。

因此,将所有的scrollview.findViewById()and更改layout.findViewById()为只调用不findViewById()带预定义限定符的调用。这样,您将获得对用户按下按钮时正在查看的实际布局对象的引用。

于 2012-09-25T23:28:33.377 回答