Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 滚动条内部布局视图的动态生成_Android_Scrollview_Layout Inflater - Fatal编程技术网

Android 滚动条内部布局视图的动态生成

Android 滚动条内部布局视图的动态生成,android,scrollview,layout-inflater,Android,Scrollview,Layout Inflater,我面临一个问题,我需要在scrollview中一个接一个地生成动态视图。应在单击按钮后生成视图 对于第一次单击按钮,它工作正常,但在第二次单击后失败,错误为“java.lang.IllegalStateException:ScrollView只能承载一个直接子级” 谁能帮我把这个修好吗 我的主要活动布局,其中scrollview是activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/

我面临一个问题,我需要在scrollview中一个接一个地生成动态视图。应在单击按钮后生成视图

对于第一次单击按钮,它工作正常,但在第二次单击后失败,错误为“java.lang.IllegalStateException:ScrollView只能承载一个直接子级”

谁能帮我把这个修好吗

我的主要活动布局,其中scrollview是activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/parentRL">
    <Spinner android:id="@+id/spinState" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <Spinner android:id="@+id/spinCity" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@id/spinState"/>
    <TextView android:id="@+id/cityCount" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/spinCity" android:layout_below="@id/spinState" android:layout_marginLeft="5dp" 
        android:textIsSelectable="false"/>
    <Button android:id="@+id/showAddress" android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_toRightOf="@id/spinState" android:onClick="populateAddress" android:text="Search"/>
    <ScrollView android:id="@+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_below="@id/spinCity">
    </ScrollView>
</RelativeLayout>

android scrollview只支持child,因此它抛出java.lang.IllegalStateException。 您应该在scrollview中添加一个LinearLayout或相对布局,然后将子级添加到该布局中

    <ScrollView android:id="@+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_below="@id/spinCity">
<LinearLayout android:id="@+id/layout" 
android:layout_width="match_parent" android:layout_height="wrap_content">
    </LinearLayout>
    </ScrollView>

LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.address_layout, null);
TextView _c_ = (TextView)(myView.findViewById(R.id.center));
_c_.setVisibility(View.VISIBLE);
_c_.setText(my_random_number);
View insertPoint = findViewById(R.id.scView);
((ViewGroup) insertPoint).addView(myView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    <ScrollView android:id="@+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_below="@id/spinCity">
<LinearLayout android:id="@+id/layout" 
android:layout_width="match_parent" android:layout_height="wrap_content">
    </LinearLayout>
    </ScrollView>