Why am I getting a NullPointerException when using setCurrentText or setText for TextSwitcher?

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

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

This code works fine if twText is a TextView . But it returns a NullPointerException using 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 get 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 added twText.removeAllViews() to twText.setFactory() . It works, but it does not look like the best way to remove and add views again. How can I check if Views are already there?

Also, I would like to keep the formatting / style of my TextSwitcher in xml. Using setFactory does not allow me to do this. What can I do?

+6
source share
2 answers

It looks like no views were added to the text switch at the time setCurrentText called. This method basically just gets the current text view and sets the text for it, so if a NullPointerException occurs, this can only happen due to the lack of representations in the text switch.

As for the error with factory: you have to be careful either to set the factory, or add two views yourself. You have one of the following options:

  • A call to setFactory , which will create two views.
  • Add two views manually by calling addView .

In both cases, there can be only two species, nothing more. If you install a factory and some views have already been added, you will receive an error message. You can check if you need to set the factory by checking what the 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); } }); } 
+6
source

It may be too late, but I found your question while I was looking for the answer to my question. I was getting a similar error, cannot add more than two views ... to an empty TextSwitcher when trying to call setFactory. My problem was resolved when I changed my activity to an Activity extension instead of an ActionBarActivity. Hope this helps some people in the future.

Throw TextSwitcher and more than 2 views "error when calling setFactory ()

0
source

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


All Articles