3

我通过这样的代码创建了一些 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 得到一个空指针,因为我不使用这种方法。我的错误在哪里?

4

4 回答 4

18

null在提供给适配器的数据列表/数组中有值。ArrayAdapter将为每一行调用toString此数据,如果是nullaNullPointerException将被抛出。

确保您没有nullspinnerA(我认为这是第二个的数据Spinner(!??!))。

于 2012-07-31T13:44:38.877 回答
1

您使用的第一个微调器:

spinnerArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item);

第二你使用:

spinnerAdapter.setDropDownViewResource(com.Orange.R.layout.multiline_spinner_dropdown_item);

您确定两者都相同/工作正常吗?

于 2012-07-31T13:47:16.047 回答
0

我的问题是自定义项目布局和下拉项目布局没有对主文本视图使用相同的 ID;基本上是因为我想创建一个自定义项,但保持默认的下拉项。

创建具有相同 textview id 的自定义下拉项解决了该问题。

于 2013-06-27T00:07:43.907 回答
0

嘿,我已经使用此功能完成了它,检查空值,不要将它们添加到您的列表中。此函数将返回表中指定列的所有值的列表数组,而不是空值。

public List<String> getList()
{
    List<String> list = new ArrayList<String>();


    Cursor cursor = sqlDb.query("tablename", new String[] {
            "someColumnName1" }, null, null, null, null, null);

        int i=0;
        for (cursor.moveToPosition(i); !cursor.isAfterLast(); cursor.moveToNext()) {
           if(cursor.getString(i) != null)   //in loop check for Null Values, add only Not null values
           {
               list.add(cursor.getString(i));
           }
        }


    return shopList;

}

从上述方法填充微调器,如下所示:

private void populateSpinnerList()
{
    List<String> list = getList();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
            R.layout.some_layout, R.id.some_textview_in_layout, list);
    if (!list.equals(""))
        spinner.setAdapter(dataAdapter);
}

它对我来说就像一个魅力。

于 2013-10-01T05:21:13.617 回答