我试图添加dinamically spinner和两个editText,但我有一个例外。我怀疑这行代码:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.id.simple_spinner_item,items);
因为当我试图显示一个空的微调器和两个editTexts时,它工作得很好,但是当向微调器添加值时,它会崩溃。我认为问题来自:
R.id.simple_spinner_item
,我在xml文件中添加了简单的simple_spinner_item。
这是我的java代码:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.SSIDZone);
String[] items = new String[]{ "3","4","5","6","7","8","9","10","11","12","13"};
for (int i =0;i<number;i++)
{ LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
Spinner spin = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.id.simple_spinner_item,items);//suspected line
LayoutParams lp3 = new LayoutParams();
l.addView(spin,lp3);
spin.setAdapter(adapter);
EditText text1 = new EditText(this);
LayoutParams lp1 = new LayoutParams();
l.addView(text1,lp1);
EditText text2 = new EditText(this);
LayoutParams lp2 = new LayoutParams();
l.addView(text2,lp2);
linearLayout.addView(l);
}
这是我的xml布局:
<LinearLayout
android:id="@+id/SSIDZone"
android:layout_width="312dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="88dp"
android:layout_weight="0.46"
android:background="#30ad9e"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/simple_spinner_item"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
最后这是我的LogCat:
05-27 15:36:25.989: E/AndroidRuntime(24818): FATAL EXCEPTION: main
05-27 15:36:25.989: E/AndroidRuntime(24818): android.content.res.Resources$NotFoundException: Resource ID #0x7f050053 type #0x12 is not valid
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2184)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.content.res.Resources.getLayout(Resources.java:872)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.Spinner.onMeasure(Spinner.java:439)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1059)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1220)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.ScrollView.onMeasure(ScrollView.java:321)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-27 15:36:25.989: E/AndroidRuntime(24818): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.View.measure(View.java:15635)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.Choreographer.doFrame(Choreographer.java:548)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.os.Handler.handleCallback(Handler.java:800)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.os.Handler.dispatchMessage(Handler.java:100)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.os.Looper.loop(Looper.java:194)
05-27 15:36:25.989: E/AndroidRuntime(24818): at android.app.ActivityThread.main(ActivityThread.java:5371)
05-27 15:36:25.989: E/AndroidRuntime(24818): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 15:36:25.989: E/AndroidRuntime(24818): at java.lang.reflect.Method.invoke(Method.java:525)
05-27 15:36:25.989: E/AndroidRuntime(24818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-27 15:36:25.989: E/AndroidRuntime(24818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-27 15:36:25.989: E/AndroidRuntime(24818): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Oups愚蠢的问题!!我没有得到足够的重视。这是一个有效的代码,以防有人需要它。
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.SSIDZone);
String[] items = new String[]{ "3","4","5","6","7","8","9","10","11","12","13"};
for (int i =0;i<number;i++)
{ LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
Spinner spin = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,items);//suspected line
LayoutParams lp3 = new LayoutParams();
l.addView(spin,lp3);
spin.setAdapter(adapter);
EditText text1 = new EditText(this);
LayoutParams lp1 = new LayoutParams();
l.addView(text1,lp1);
EditText text2 = new EditText(this);
LayoutParams lp2 = new LayoutParams();
l.addView(text2,lp2);
linearLayout.addView(l);
}
这是xml文件:
<LinearLayout
android:id="@+id/SSIDZone"
android:layout_width="312dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="88dp"
android:layout_weight="0.46"
android:background="#30ad9e"
android:orientation="vertical" >
</LinearLayout>