TextSwitcher throwing “more than 2 views” error when setFactory() is called

霸气de小男生 提交于 2019-12-12 01:43:40

问题


I am working on an assignment for an Android Programming Class. I am not looking to spoon-fed code but I cannot figure out what is causing the problem.

The specifics of the assignment requires me to have a ViewSwitcher that contains a ListView and multiple TextViews (on the second view) for showing data from the item in the list when the item is clicked. To populate the ListView we have to use TextSwitchers to animate the text when it is displayed/changed. I can get the app to work in the simulator with either the ViewSwitcher or the TextSwitchers but not both. The error is thrown and app terminated when the TextSwitcher.setFactory() method is called; if I do not call .setFactory() the program continues without error.

I have asked my instructor and classmates for help and only one classmate has responded since I asked for help on Monday. This assignment is now more than one week late so I'm just assuming that I will get a zero at this point, however, the next project builds off of this one so I still need to finish it.

Forgoing posting xml in favor of actual code:

public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewGroup listItem = null;
        TextSwitcher stockNameTS = null;
        TextSwitcher stockSymbolTS = null;
        TextSwitcher stockPriceTS = null;
        StockInfo stockInfo = null;

        if(convertView == null)
        {
            listItem = (ViewGroup) getLayoutInflator().inflate(R.layout.list_item, null);
            listItem.setTag(getParent());
            stockInfo = stockList.get(position);

            //Get TextSwitchers
            stockNameTS = (TextSwitcher) listItem.findViewById(R.id.stockNameTS);
            stockSymbolTS = (TextSwitcher) listItem.findViewById(R.id.stockSymbolTS);
            stockPriceTS = (TextSwitcher) listItem.findViewById(R.id.stockPriceTS);

            stockNameTS.setFactory(new ViewFactory() {//throws here

                @Override
                public View makeView() {
                    TextView tv = new TextView(MainActivity.this);
                    tv.setTextSize(20);
                    return tv;
                }
            });
            stockSymbolTS.setFactory(new ViewFactory() {

                @Override
                public View makeView() {
                    TextView tv = new TextView(MainActivity.this);
                    tv.setTextSize(20);
                    return tv;
                }
            });
            stockPriceTS.setFactory(new ViewFactory() {

                @Override
                public View makeView() {
                    TextView tv = new TextView(MainActivity.this);
                    tv.setTextSize(20);
                    return tv;
                }
            });

            //Set animations
            stockNameTS.setInAnimation(MainActivity.this, android.R.anim.slide_in_left);
            stockNameTS.setOutAnimation(MainActivity.this, android.R.anim.slide_out_right);
            stockSymbolTS.setInAnimation(MainActivity.this, android.R.anim.slide_in_left);
            stockSymbolTS.setOutAnimation(MainActivity.this, android.R.anim.slide_out_right);
            stockPriceTS.setInAnimation(MainActivity.this, android.R.anim.slide_in_left);
            stockPriceTS.setOutAnimation(MainActivity.this, android.R.anim.slide_out_right);
        }
        else
        {
            listItem = (ViewGroup) convertView; 
        }

        stockNameTS.setText(stockInfo.getName());
        stockSymbolTS.setText(stockInfo.getSymbol());
        stockPriceTS.setText(USDFormat.format(stockInfo.getPrice()));

        return listItem;
    }

Thank you!


回答1:


A fellow classmate took a look at the project and said my problem was that my MainActivity extended ActionBarActivity instead of Activity. I don't see why Android would make the distinction between that, but it does and changing to extending Activity solved the problem.



来源:https://stackoverflow.com/questions/24176727/textswitcher-throwing-more-than-2-views-error-when-setfactory-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!