setAdapter在Fragment中的onItemSelected中崩溃应用程序

时间:2014-02-01 21:00:06

标签: java android dataadapter

我希望我的应用程序在其他微调器选择项目更改时更改其他微调器项目但是当我为调整器应用程序崩溃时设置了适配器。顺便说一下,当app首先启动时,我认为它会调用onItemSelectd方法4次。可能是问题吗?这是我的代码:

@Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
        {
            ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);

            if( parent.getId() == R.id.aySpinner)
            {               
                Spinner gunSpinner = (Spinner) view.findViewById(R.id.gunSpinner);


                // An item was selected. You can retrieve the selected item using
                String ay = (String) parent.getItemAtPosition(pos);

                List<String> list = new ArrayList<String>();

                if( ay.equals("Ocak"))
                {

                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Şubat") )
                {

                    for(int i = 1; i<=28; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }
                }
                else if( ay.equals("Mart") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Nisan") )
                {
                    for(int i = 1; i<=30; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Mayıs") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Haziran") )
                {
                    for(int i = 1; i<=30; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }
                }
                else if( ay.equals("Temmuz") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Ağustos") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Eylül") )
                {
                    for(int i = 1; i<=30; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }
                else if( ay.equals("Ekim") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }   

                }
                else if( ay.equals("Kasım") )
                {
                    for(int i = 1; i<=30; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }
                }
                else if( ay.equals("Aralık") )
                {
                    for(int i = 1; i<=31; ++i )
                    {
                        String foo = Integer.toString(i);
                        list.add(foo);
                    }

                }

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            gunSpinner.setAdapter(dataAdapter);

            }

        }

*的 修改

我已经解决了这个问题。我不知道为什么会出现这个问题,但是当我改变这一行时:

Spinner gunSpinner = (Spinner) view.findViewById(R.id.gunSpinner);

到此:

Spinner gunSpinner = (Spinner) getActivity().findViewById(R.id.gunSpinner);

它有效。感谢帮助和抱歉不必要的帖子。

0 个答案:

没有答案