2

我正在尝试将文本视图和进度条添加到滚动视图中,但它给了我一个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: 

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

4

2 回答 2

4

ScrollView 必须只有一个 Direct 子级

如果您尝试添加如下

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

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

解决方案:

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

而且在您的代码中,方法名称以大写字母给出,记住 java 区分大小写。我认为您只是为概述提供上述代码。

于 2013-04-15T08:17:35.940 回答
1

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

scrollView.removeAllViews();

如果您没有类似的东西,请删除第一个视图,反正只有一个。

scrollView.removeView(0);

删除所有视图

于 2013-04-15T08:57:14.650 回答