Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:54+00:00 2026-05-22T15:36:54+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:36:54+00:00Added an answer on May 22, 2026 at 3:36 pm

    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);
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I update the text of my TextSwitcher in the code of setViewValue. But in
My TextSwitcher for each record in ListView should display first value ( text1 )
I can't manage to get my textSwitcher to be able to support superscript text.
So I have a TextSwitcher that I want to update every second with the
The layout is basic: an EditText at the top followed by a ListView. EditText
I want to implement search bar in my application which will filter list view
I am having a problem in searching a ListView. In my activity I have
I have an Activity with an AutoComplete box on it. Whenever the text changes,
I am really stuck on this. I am trying to do a simple textswitcher
I need to detect text changes in an EditText. I've tried TextWatcher, but it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.