Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get NullPointerException when use setCurrentText or setText for TextSwitcher?

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?

like image 481
LA_ Avatar asked May 22 '11 10:05

LA_


1 Answers

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);
        }
    });
}
like image 111
Malcolm Avatar answered Oct 10 '22 08:10

Malcolm