gpt4 book ai didi

android - 以编程方式将元素添加到 ScrollView 给我 Java.Lang.IllegalStateException

转载 作者:太空狗 更新时间:2023-10-29 16:42:08 27 4
gpt4 key购买 nike

我正在尝试将 TextView 和进度条添加到 ScrollView ,但它给了我一个 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);

堆栈:

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:

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

最佳答案

ScrollView must have only one Direct child

如果你想像下面那样添加

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

它会抛出异常说 ScrollView 必须只有一个直接 child

解决方案:

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

And also in your code, method names are given in Capital Letters, Remember java is Case Sensitive. I think you are just giving the above code for the overview.

关于android - 以编程方式将元素添加到 ScrollView 给我 Java.Lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com