一切!我在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 ?
谢谢, 嫩
这是在按下调用我添加了微调器创建方法的按钮后的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 btw。)
谢谢, 嫩
答案 0 :(得分:1)
你需要获得布局
View linearLayout = findViewById(R.id.layoutID);
Crate spinner
Spinner spinner = new Spinner(this);
//Make sure you have valid layout parameters.
spinner .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, spinnerList);
spinner.setAdapter(spinnerArrayAdapter);
然后添加微调器以查看
((LinearLayout) linearLayout).addView(spinner );