TextSwitcher投掷"超过2次"错误时setFactory()被调用 [英] TextSwitcher throwing "more than 2 views" error when setFactory() is called

查看:397
本文介绍了TextSwitcher投掷"超过2次"错误时setFactory()被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个Android编程类的分配。我不希望填鸭式地灌输code,但我想不出是什么原因造成的问题。

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.

分配的细节要求我有包含为被点击的项目时表示从项目列表中的数据一个ListView和多个TextViews(在第二视图)一个ViewSwitcher。要填充我们必须使用TextSwitchers显示时,动画的文字的ListView /改变。我可以得到应用在模拟器中工作,无论是ViewSwitcher或TextSwitchers但不能同时使用。错误被抛出,当TextSwitcher.setFactory()方法被调用的应用程序终止;如果我不叫.setFactory()程序可以继续没有错误。

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.

放弃发布XML有利于实际code的:

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

感谢您!

推荐答案

一个他的同学看了看这个项目,说我的问题是,我MainActivity扩展,而不是活动ActionBarActivity。我不明白为什么会的Andr​​oid作出这样的区分,但它确实和不断变化的延长活动解决了这个问题。

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.

这篇关于TextSwitcher投掷"超过2次"错误时setFactory()被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆