我通过这样的代码创建了一些 Spinner:
if (dyn_list_id[i]==null){
String[] form_ids = db.getEntireColumn(list_table_name[i], "form_label_id");
String[] spinnerArray = new String[form_ids.length];
for (int k=0;k<form_ids.length;k++){
spinnerArray[k] = db.getFromLanguageTable(language_short_name, list_table_name[i], form_ids[k]);
System.out.println("Spinner Array "+spinnerArray[k]);
}
product_spinner[i] = new Spinner(viewToLoad.getContext()); //First Spinner
product_spinner[i].setLayoutParams(lp_spinner);
product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(viewToLoad.getContext(), com.Orange.R.layout.my_spinner_textview, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);
product_spinner[i].setAdapter(spinnerArrayAdapter);
this.SpinnerFields.add(product_spinner[i]);
linear_layout[i].addView(product_spinner[i]);
tablerow_product[i].addView(linear_layout[i]);
Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
else {
String[] spinnerA = new String[item_visible];
int cont=0;
for (int p=0;p<status_itemList.length;p++){
if (status_itemList[p].trim().equals("1")){
spinnerA[cont]=db.getFromLanguageTable(language_short_name, "pr_dyn_list_option", form_label_id_itemList[p]);
System.out.println("Spinner dinamic array "+spinnerA[cont]);
cont++;
}
}
product_spinner[i] = new Spinner(viewToLoad.getContext()); //second Spinner
product_spinner[i].setLayoutParams(lp_spinner);
product_spinner[i].setBackgroundResource(R.drawable.spinner_selector);
ArrayAdapter spinnerAdapter = new ArrayAdapter(viewToLoad.getContext(), com.Orange.R.layout.my_spinner_textview, spinnerA);
spinnerAdapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item); product_spinner[i].setAdapter(spinnerAdapter);
this.SpinnerFields.add(product_spinner[i]);
linear_layout[i].addView(product_spinner[i]);
tablerow_product[i].addView(linear_layout[i]);
Themes_TableLayout.addView(tablerow_product[i],new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
它在屏幕上看起来很好,但是当我点击它时,我得到了这个错误:
15:53:22.040 8716 com.Orange ERROR AndroidRuntime java.lang.NullPointerException
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.AbsListView.obtainView(AbsListView.java:1294)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.ListView.onMeasure(ListView.java:1109)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.view.View.measure(View.java:8171)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.os.Looper.loop(Looper.java:123)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:4627)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:521)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
15:53:22.040 8716 com.Orange ERROR AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
添加的第一个 Spinner 工作正常,但对于第二个 Spinner,我收到此错误。我不知道为什么我在 AdapterArray.createViewFromResources 得到一个空指针,因为我不使用这种方法。我的错误在哪里?