【问题标题】:Inflate a ListView inside a OnItemSelectedListener that is inside a Fragment - java.lang.Null Error in setAdapter在片段内的 OnItemSelectedListener 内膨胀 ListView - setAdapter 中的 java.lang.Null 错误
【发布时间】:2014-10-07 15:58:58
【问题描述】:

我快疯了,我收到这个错误:在这一行 listView.setAdapter(dataClassificationAdapter); :

   10-07 08:03:24.592: E/AndroidRuntime(2396): FATAL EXCEPTION: main
   10-07 08:03:24.592: E/AndroidRuntime(2396): Process: com.tfg.webquest, PID: 2396
   10-07 08:03:24.592: E/AndroidRuntime(2396): java.lang.NullPointerException
   10-07 08:03:24.592: E/AndroidRuntime(2396):  at com.tfg.webquest.HomeActivity$ClassificationTab.onCreateView(HomeActivity.java:698)

还有更多...

我有这个:

public class HomeActivity extends ActionBarActivity implements ActionBar.TabListener, OnItemSelectedListener {

public static class ClassificationTab extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home_classification, container,false);

        //Get the subject spinner
        spinnerSubjectClassification = (Spinner) rootView.findViewById(R.id.home_classification_spinner);
        //subjectSpinnerArray will show "(Choose a subject)"+all the subjects 
       String[] subjectSpinnerArray = new String[ss.length+1];
        //Insert in the subjectSpinnerArray "(Choose a subject)"
        subjectSpinnerArray[0]=getString(R.string.home_select_subject_string);
        //Insert in the subjectSpinnerArray all the subjects    
        for(int i=0;i<ss.length;i++){
            subjectSpinnerArray[i+1]=ss[i];
        }
        //Insert the information of the array on a list so as to introduce it in the adapter
        List<String> list = new ArrayList<String>();
             for(int i=0;i<subjectSpinnerArray.length;i++){
                    list.add(subjectSpinnerArray[i]);
              }
                ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
                //Dropdown the adapter
                dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //Show the adapter
                spinnerSubjectClassification.setAdapter(dataAdapter2);    
             //It activates the retrocalled method, called when we choose a subject
                spinnerSubjectClassification.setOnItemSelectedListener(mySubjectClassificationListener);
    }


    private OnItemSelectedListener mySubjectClassificationListener = new Spinner.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
            //Fill the subjectList
            for(int i=0;i<classification[pos-1].length;i++){
                ClassificationModel classificationModel = new ClassificationModel(classification[pos-1][i]);
                classificationList.add(classificationModel);
            }


            //create an ArrayAdaptar from the String Array
            dataClassificationAdapter = new MyCustomAdapter(ctx,R.layout.classification,classificationList);

            ListView listView = (ListView) getView().findViewById(R.id.classification_list);

            ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(getActivity(), 
                    android.R.layout.simple_list_item_2, classification[pos-1]);

            ListView listView = (ListView) getView().findViewById(R.id.listview_classification); 

            // Assign adapter to ListView

            listView.setAdapter(dataClassificationAdapter);
        }
    }

}

我需要在 onClickListener 中为 listView 充气,因为 listview 的内容取决于 spinner selected item 。 感谢您的宝贵时间。

【问题讨论】:

  • HomeActivity 的第 698 行是什么?
  • listView.setAdapter(dataClassificationAdapter);
  • 那么你的 listView 为空。 ListView listView = (ListView) getView().findViewById(R.id.classification_list); 这条线导致了问题。请注意您是如何在那里创建 2 个列表视图变量的。
  • 感谢您纠正该错误。但是我的问题仍然存在:我在删除一个listView的同一行中有同样的错误,使用:ListView listView = (ListView) getView().findViewById(R.id.listview_classification);
  • 确保在更改某些内容后继续编辑您的答案。你说你正在使用“rootView”。在该方法上 rootView 不存在。另外你确定listview_classification 存在吗?

标签: android listview fragment android-listfragment onitemclicklistener


【解决方案1】:

getView().findViewById() 替换为getActivity().findViewById()

但您应该考虑将您的 ListView 创建移动到 onCreateView(然后使用 rootView.findViewById())并且在 OnItemSelected 中仅更改适配器内的数据

【讨论】:

  • 即使使用 getActivity().findViewById() 我的错误仍然发生
  • 但我无法在 OnCreate 中为 listView 充气,因为首先我需要选择一个微调器项目,所以我必须从侦听器中为 listView 充气
  • 您确定在您的 R.layout.fragment_home_classification 布局中有 ID 为 R.id.classification_list 和 R.id.classification_list 的列表视图吗?
  • 通过使用 findViewById 你不会膨胀一个视图只获取它的实例。如果视图已经在布局中的视图层次结构中,则可以使用 findViewBYId,如果要向布局添加新的列表视图,则需要使用 LayoutInflater 或简单地将子视图添加到根视图
  • 我拥有的是布局中的 listView。我还有一个适配器,它可以填充另一个填充 textView 的布局,这是一个列表项的内容。
猜你喜欢
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多