以编程方式向scrollview添加元素给了我Java.Lang.IllegalStateException

时间:2013-04-15 08:13:45

标签: android android-layout xamarin.android

我正在尝试将textview和progressbar添加到滚动视图中,但它为我提供了Java.Lang.IllegalStateException:

我正在使用xamarin所以这不是java而是c#但是它是相同的原则

这是代码(摘要):

    SetContentView(Resource.Layout.Usage);
    ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scrollView1);
    LinearLayout linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout2);

while (...){
    TextView textView = new TextView(this);
    textView.Text = "Some text";
    textView.SetTextSize(Android.Util.ComplexUnitType.Sp, 18.0f);
    linearLayout.AddView(textView);

    ProgressBar progressBar = new ProgressBar(this, null, Android.Resource.Attribute.ProgressBarStyleHorizontal);
    progressBar.Progress = 25;
    linearLayout.AddView(progressBar);
}

scrollView.AddView(linearLayout);

Stack:

04-15 08:49:00.603 I/MonoDroid( 1389): UNHANDLED EXCEPTION: Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
04-15 08:49:00.603 I/MonoDroid( 1389): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00023] in /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
04-15 08:49:00.603 I/MonoDroid( 1389): at Android.Views.ViewGroup.AddView (Android.Views.View) [0x0003e] in /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.ViewGroup.cs:1186
04-15 08:49:00.603 I/MonoDroid( 1389): at MyOrangeDroid.Usage.OnCreate (Android.OS.Bundle) [0x001be] in c:\Users\clement\Documents\Visual Studio 2012\Projects\MyOrangeAndroid\MyOrangeDroid\Usage.cs:70
04-15 08:49:00.603 I/MonoDroid( 1389): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.App.Activity.cs:1490
04-15 08:49:00.603 I/MonoDroid( 1389): at (wrapper dynamic-method) object.6c050c56-6d80-48bc-9425-e832938cdfa7 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
04-15 08:49:00.603 I/MonoDroid( 1389): 
04-15 08:49:00.603 I/MonoDroid( 1389):   --- End of managed exception stack trace ---
04-15 08:49:00.603 I/MonoDroid( 1389): java.lang.IllegalStateException: ScrollView can host only one direct child
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.widget.ScrollView.addView(ScrollView.java:211)
04-15 08:49:00.603 I/MonoDroid( 1389):  at myorangedroid.Usage.n_onCreate(Native Method)
04-15 08:49:00.603 I/MonoDroid( 1389):  at myorangedroid.Usage.onCreate(Usage.java:28)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.os.Looper.loop(Looper.java:123)
04-15 08:49:00.603 I/MonoDroid( 1389):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-15 08:49:00.603 I/MonoDroid( 1389):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 08:49:00.603 I/MonoDroid( 1389):  at java.lang.reflect.Method.invoke(Method.java:507)
04-15 08:49:00.603 I/MonoDroid( 1389):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-15 08:49:00.603 I/MonoDroid( 1389):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-15 08:49:00.603 I/MonoDroid( 1389):  at dalvik.system.NativeStart.main(Native Method)
In mgmain JNI_OnLoad
04-15 08:49:01.503 W/dalvikvm( 1389): JNI WARNING: JNI method called with exception raised
04-15 08:49:01.503 W/dalvikvm( 1389):              in Lmyorangedroid/Usage;.n_onCreate (Landroid/os/Bundle;)V (CallObjectMethod)
04-15 08:49:01.503 W/dalvikvm( 1389): Pending exception is:
04-15 08:49:01.503 I/dalvikvm( 1389): Ljava/lang/IllegalStateException;: ScrollView can host only one direct child
04-15 08:49:01.513 I/dalvikvm( 1389):   (no stack trace data found)
04-15 08:49:01.513 I/dalvikvm( 1389): "main" prio=5 tid=1 NATIVE
04-15 08:49:01.513 I/dalvikvm( 1389):   | group="main" sCount=0 dsCount=0 obj=0x4001f1a8 self=0xce48
04-15 08:49:01.513 I/dalvikvm( 1389):   | sysTid=1389 nice=0 sched=0/0 cgrp=default handle=-1345006528
04-15 08:49:01.513 I/dalvikvm( 1389):   | schedstat=( 7098128094 1495743800 310 )
04-15 08:49:01.513 I/dalvikvm( 1389):   at myorangedroid.Usage.n_onCreate(Native Method)
04-15 08:49:01.523 I/dalvikvm( 1389):   at myorangedroid.Usage.onCreate(Usage.java:28)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.os.Looper.loop(Looper.java:123)
04-15 08:49:01.523 I/dalvikvm( 1389):   at android.app.ActivityThread.main(ActivityThread.java:3683)
04-15 08:49:01.523 I/dalvikvm( 1389):   at java.lang.reflect.Method.invokeNative(Native Method)
04-15 08:49:01.523 I/dalvikvm( 1389):   at java.lang.reflect.Method.invoke(Method.java:507)
04-15 08:49:01.523 I/dalvikvm( 1389):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-15 08:49:01.523 I/dalvikvm( 1389):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-15 08:49:01.523 I/dalvikvm( 1389):   at dalvik.system.NativeStart.main(Native Method)
04-15 08:49:01.523 I/dalvikvm( 1389): 
04-15 08:49:01.523 E/dalvikvm( 1389): VM aborting
04-15 08:49:01.533 I/mono    ( 1389): Stacktrace:
04-15 08:49:01.533 I/mono    ( 1389): 
04-15 08:49:01.533 I/mono    ( 1389):   at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr) [0x00000] in /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:129
04-15 08:49:01.533 I/mono    ( 1389):   at Java.Lang.Throwable.get_Message () [0x0003e] in /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Lang.Throwable.cs:195
04-15 08:49:01.533 I/mono    ( 1389):   at (wrapper runtime-invoke) <Module>.runtime_invoke_object__this__ (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Unhandled Exception:

Java.Lang.IllegalStateException: 

感谢您的帮助/阅读: - )

2 个答案:

答案 0 :(得分:4)

  

ScrollView必须只有一个Direct子

如果您尝试添加如下

layout.AddView(textView);
layout.AddView(progressBar);

它将抛出Exception,说ScrollView必须只有一个直接子项

<强>解决方案:

获取LinearLayout并向其添加textView和progressbar,然后将LinearLayout添加到ScrollView

  

在你的代码中,方法名称在Capital Letters中给出,记住java是区分大小写的。我想你只是给出了上面的代码来概述。

答案 1 :(得分:1)

你的xml滚动视图里面有什么东西吗?如果没有任何内容,请在添加新视图之前尝试删除所有视图。这是java,但你必须在c#中有一个等价物。

scrollView.removeAllViews();

如果您没有这样的东西,请删除第一个视图,无论如何只会有一个。

scrollView.removeView(0);

Remove all views