6

TextSwitcher is part of ListView row (defined in xml). I define the text of TextSwitcher in the setViewValue:

switch(viewId) {
case R.id.tw_text:
    TextSwitcher twText = (TextSwitcher) view;
    twText.setCurrentText("test text");
    return true;

This code works perfectly if twText is TextView. But it returns NullPointerException with TextSwitcher:

05-22 10:24:08.781: ERROR/AndroidRuntime(3348): FATAL EXCEPTION: main
05-22 10:24:08.781: ERROR/AndroidRuntime(3348): java.lang.NullPointerException
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.TextSwitcher.setCurrentText(TextSwitcher.java:89)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at my.package.MainActivity$1.setViewValue(MainActivity.java:87)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:126)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.ListView.onMeasure(ListView.java:1109)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.View.measure(View.java:8171)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.os.Looper.loop(Looper.java:123)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at java.lang.reflect.Method.invoke(Method.java:521)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-22 10:24:08.781: ERROR/AndroidRuntime(3348):     at dalvik.system.NativeStart.main(Native Method)

Update 1: looks like I need to add setFactory() to my code. But if I do the following:

  TextSwitcher twText = (TextSwitcher) view;
  twText.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
      TextView t = new TextView(MainActivity.this);
      return t;
      }
      });

I am getting another error:

05-22 10:54:42.852: ERROR/AndroidRuntime(3907): java.lang.IllegalStateException: Can't add more than 2 views to a ViewSwitcher
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.ViewSwitcher.addView(ViewSwitcher.java:64)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.widget.TextSwitcher.addView(TextSwitcher.java:67)
05-22 10:54:42.852: ERROR/AndroidRuntime(3907):     at android.view.ViewGroup.addView(ViewGroup.java:1845)

Update 2: I've added twText.removeAllViews() before twText.setFactory(). It works, but looks like not the best way to delete and add Views again. How can I check if Views are already there?

Also, I would like to keep formatting/style of my TextSwitcher in the xml. Usage of setFactory doesn't allow me to do so. What can I do?

LA_
  • 19,823
  • 58
  • 172
  • 308

2 Answers2

6

It looks like no views have been added to the text switcher at the point when setCurrentText is called. This method basically just gets current text view and sets the text for it, so if a NullPointerException appears, it may only happen due to the absence of the views in the text switcher.

Regarding the error with the factory: you have to be careful to either set a factory or to add two views yourself. You have either of these options:

  • Call setFactory which will create two Views.
  • Add two views manually by calling addView.

In both cases there can be only two views, no more no less. If you set a factory and some views have already been added, you will get an error. You can check if you need to set a factory by checking what getChildCount() method returns:

TextSwitcher switcher = (TextSwitcher) view;
if (switcher.getChildCount() != 2) {
    switcher.removeAllViews();
    switcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return new TextView(MainActivity.this);
        }
    });
}
Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • I've tested that both with `setCurrentText` and `setText` - the error is the same (so, I've just taken old code and new log ;). You are right that I haven't added any View there (strange, that it is not added by default). But how can I add it? Please see my updated question. – LA_ May 22 '11 at 10:55
  • thanks for your help. I don't use `addView`. So, looks like `View`s are added automatically, since I use `TextSwitcher` in my xml. But somehow I can not use them. Or, this `setFactory` code is called several times (but my understanding was that each time it creates new Views there). I've updated the question again. – LA_ May 22 '11 at 11:18
  • what should I do to force TextSwitcher views to use style (font size, color, position etc.) of TextSwitcher defined in xml? – LA_ May 22 '11 at 11:36
  • When you apply a style to a `ViewGroup`, its children don't inherit its style anyway. Therefore, the best way to apply a specific style is to define your `TextView` in a separate XML file and inflate it in the factory. – Malcolm May 22 '11 at 15:22
0

Probably a little too late on this but I found your question while I was searching for an answer to mine. I was getting a similar error, can't add more than 2 views..., in an empty TextSwitcher when trying to call setFactory. My issue was resolved when I changed my activity to extending Activity instead of ActionBarActivity. Hopefully this will help some people in the future.

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

Community
  • 1
  • 1