TextSwitcher throws a "more than 2 views" error when calling setFactory ()

I am working on a task for the Android programming class. I am not looking for code with a spoon, but I cannot figure out what causes the problem.

The specifics of the job requires that I have a ViewSwitcher that contains a ListView and several TextViews (in the second view) to display data from an item in the list when the item is clicked. To populate the ListView, we must use TextSwitchers to animate the text when it is displayed / modified. I can get the application to work in the simulator using ViewSwitcher or TextSwitchers, but not with both. An error is thrown and the application terminates when the TextSwitcher.setFactory () method is called; if I do not call .setFactory (), the program continues without errors.

I asked my instructor and classmates for help, and only one classmate answered, since I asked for help on Monday. This appointment is now more than one week old, so I just assume that at that moment I will get zero, however the next project will be built from this, so I still need to finish it.

Submitting xml publication in favor of valid 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; } 

Thanks!

0
source share
1 answer

One classmate took a look at the project and said that my problem was that my MainActivity expanded ActionBarActivity instead of Activity. I donโ€™t understand why Android will distinguish between this, but it does this and proceeds to expand the Activity by solving the problem.

0
source

Source: https://habr.com/ru/post/888759/


All Articles