以编程方式将 Spinner 添加到 LinearLayout
全部!我在 Android 世界中还是个新手,但在在这里询问之前,我确实尽可能多地搜索了这个问题的答案......
我的布局中有一个水平 LinearLayout,名为“spinner”。最初(即,在 xml 文件中),那里只有一个微调器。
我还有一个按钮(id:addPath),它激活了一个方法,我希望这个方法(add_path)将另一个微调器添加到视图中(并且在再次点击后 - 另一个微调器等)。 所有额外的旋转器都是相同的,即基于相同的字符串数组(path_array)。
这就是我的 xml 的样子:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/compWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_welcome" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinners"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textCompStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_prefix" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/spinner_prompt" />
<TextView
android:id="@+id/textCompEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_postfix" />
</LinearLayout>
<Button
android:id="@+id/addPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add_path"
android:text="@string/add_path_button" />
</LinearLayout>
</ScrollView>
这就是我在 onCreate 中激活微调器的方式:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.path_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
现在,add_path 方法应该是什么样子,既用于创建微调器,又用于将其实际添加到 LinearLayout?
谢谢, Arnon
这是点击按钮后的 LogCat 输出,该按钮调用了我添加了微调器创建的方法:(
02-10 19:56:17.097: D/AndroidRuntime(284): Shutting down VM
02-10 19:56:17.097: W/dalvikvm(284): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-10 19:56:17.097: E/AndroidRuntime(284): Uncaught handler: thread main exiting due to uncaught exception
02-10 19:56:17.117: E/AndroidRuntime(284): java.lang.IllegalStateException: Could not execute method of the activity
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View$1.onClick(View.java:2031)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.performClick(View.java:2364)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.onTouchEvent(View.java:4179)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.widget.TextView.onTouchEvent(TextView.java:6541)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.dispatchTouchEvent(View.java:3709)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-10 19:56:17.117: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): Caused by: java.lang.reflect.InvocationTargetException
02-10 19:56:17.117: E/AndroidRuntime(284): at arnon.cousinator.complicated.add_path(complicated.java:48)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View$1.onClick(View.java:2026)
02-10 19:56:17.117: E/AndroidRuntime(284): ... 23 more
02-10 19:56:17.117: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
02-10 19:56:17.117: E/AndroidRuntime(284): ... 27 more
02-10 19:56:17.148: I/dalvikvm(284): threadid=7: reacting to signal 3
02-10 19:56:17.148: I/dalvikvm(284): Wrote stack trace to '/data/anr/traces.txt'
02-10 20:01:17.165: I/Process(284): Sending signal. PID: 284 SIG: 9
02-10 20:01:17.638: D/dalvikvm(290): GC freed 680 objects / 54464 bytes in 54ms
02-10 20:01:17.808: D/dalvikvm(290): GC freed 40 objects / 1472 bytes in 46ms
顺便说一句,我正在使用 Eclipse。)
谢谢, 阿农
all! I'm quite new in the Android world, but I did search an answer to this problem as much as I could before asking about it here...
I have a horizontal LinearLayout in my layout, named "spinner". Initially (i.e., the in xml file), there's only one spinner there.
I also have a button (id: addPath) which activated a method and I want this method (add_path) to add another spinner to the view (and after another hit - anothr spinner, etc.). All the extra spinners are identical, i.e., are based on the same string array (path_array).
This is how my xml looks like:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/compWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_welcome" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinners"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textCompStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_prefix" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/spinner_prompt" />
<TextView
android:id="@+id/textCompEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/complicated_postfix" />
</LinearLayout>
<Button
android:id="@+id/addPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add_path"
android:text="@string/add_path_button" />
</LinearLayout>
</ScrollView>
This is how I activate the spinner within onCreate:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.path_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Now, how should the add_path method look like, both for creating the spinner and for actually adding it to the LinearLayout?
Thanks,
Arnon
Here's the LogCat output from after hitting the button that calls the method in which I've added the spinner creation:
02-10 19:56:17.097: D/AndroidRuntime(284): Shutting down VM
02-10 19:56:17.097: W/dalvikvm(284): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-10 19:56:17.097: E/AndroidRuntime(284): Uncaught handler: thread main exiting due to uncaught exception
02-10 19:56:17.117: E/AndroidRuntime(284): java.lang.IllegalStateException: Could not execute method of the activity
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View$1.onClick(View.java:2031)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.performClick(View.java:2364)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.onTouchEvent(View.java:4179)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.widget.TextView.onTouchEvent(TextView.java:6541)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View.dispatchTouchEvent(View.java:3709)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-10 19:56:17.117: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-10 19:56:17.117: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): Caused by: java.lang.reflect.InvocationTargetException
02-10 19:56:17.117: E/AndroidRuntime(284): at arnon.cousinator.complicated.add_path(complicated.java:48)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 19:56:17.117: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 19:56:17.117: E/AndroidRuntime(284): at android.view.View$1.onClick(View.java:2026)
02-10 19:56:17.117: E/AndroidRuntime(284): ... 23 more
02-10 19:56:17.117: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException
02-10 19:56:17.117: E/AndroidRuntime(284): ... 27 more
02-10 19:56:17.148: I/dalvikvm(284): threadid=7: reacting to signal 3
02-10 19:56:17.148: I/dalvikvm(284): Wrote stack trace to '/data/anr/traces.txt'
02-10 20:01:17.165: I/Process(284): Sending signal. PID: 284 SIG: 9
02-10 20:01:17.638: D/dalvikvm(290): GC freed 680 objects / 54464 bytes in 54ms
02-10 20:01:17.808: D/dalvikvm(290): GC freed 40 objects / 1472 bytes in 46ms
(I'm using Eclipse btw.)
Thanks,
Arnon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要获取布局
Crate spinner
然后添加 spinner 来查看
You need to get layout
Crate spinner
Then add spinner to view