eViewFromResource上的NullPointerEx

eViewFromResource上的NullPointerEx

本文介绍了微调框:ArrayAdapter.createViewFromResource上的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过如下代码创建一些微调器:

I create some Spinners by code like this :

    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));
}

它在屏幕上看起来不错,但是当我单击它时,出现此错误:

It appear fine on screen but when I click it I get this error :

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)

添加的第一个微调器工作正常,但是对于第二个微调器,出现此错误.我不知道为什么在AdapterArray.createViewFromResources上获得一个空指针,因为我不使用此方法.我的错误在哪里?

First Spinner added works fine, but for the second Spinner I get this error.I have no idea why I get a nullpointer at AdapterArray.createViewFromResources because I don't use this method. Where is my mistake?

推荐答案

您在提供给适配器的数据列表/数组中具有null值. ArrayAdapter将针对每一行在此数据上调用toString,如果为null,则会抛出NullPointerException.

You have null values in the data lists/array that you supply to the adapter. ArrayAdapter will call toString on this data for each row and if it is null a NullPointerException will be thrown.

确保在spinnerA中没有null值(我认为这是第二个Spinner(!??!)的数据).

Make sure you don't have null values in spinnerA(I think that is the data for the second Spinner(!??!)).

这篇关于微调框:ArrayAdapter.createViewFromResource上的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:18