Rik*_*avi 4 android textview horizontalscrollview android-linearlayout illegalstateexception
我有附加文件列表,并想让我的LinearLayout与它水平滚动.我只将一个子LinearLayout添加到我的HorizontalScrollView,除了我得到IllegalStateException.我的xml:
<HorizontalScrollView
android:id="@+id/scrollMessageFiles"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_below="@+id/editMessage"
android:orientation="horizontal"
android:weightSum="1.0" >
<LinearLayout
android:id="@+id/panelMessageFiles"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
</LinearLayout>
</HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)
并希望将文件列表添加到ScrollView中的LinearLayout,如下所示:
public void addFiles()
{
HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles);
LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles);
if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
{
for (File file: FileManagerActivity.getFinalAttachFiles())
{
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
}
scroll.addView(layout);
}
}
Run Code Online (Sandbox Code Playgroud)
除了get之外,我只向HorizontalScrollView添加一个LinearLayout
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.NewMessageActivity}: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: HorizontalScrollView can host only one direct child
at android.widget.HorizontalScrollView.addView(HorizontalScrollView.java:223)
at com.assignmentexpert.NewMessageActivity.addFiles(NewMessageActivity.java:165)
at com.assignmentexpert.NewMessageActivity.onCreate(NewMessageActivity.java:90)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
Run Code Online (Sandbox Code Playgroud)
在线
scroll.addView(layout);
Run Code Online (Sandbox Code Playgroud)
你只需要删除这一行:scroll.addView(layout);你已经在xml中声明了这个并且正在尝试再次添加它,这就是你得到多个子例外的原因.
试着运行这个:
public void addFiles() {
HorizontalScrollView scroll = (HorizontalScrollView) findViewById(R.id.scrollMessageFiles);
LinearLayout layout = (LinearLayout) findViewById(R.id.panelMessageFiles);
if(!FileManagerActivity.getFinalAttachFiles().isEmpty()) {
for (File file: FileManagerActivity.getFinalAttachFiles()) {
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
}
// This line is telling the system to add your LinearLayout to the ScrollView when it is already there, declared in your xml layout file
//scroll.addView(layout);
}
}
Run Code Online (Sandbox Code Playgroud)