Android guidance (PORTRAIT / LANDSCAPE) addTextChangedListener

1

I have a problem at the moment when my App changes its orientation from PORTRAIT to LANDSCAPE or vice versa. I tested my code and identified that the problem is in the command lines where the "editText.addTextChangedListener (new TextWatcher () {...}"; If I remove them, the App works normally, but when I put them in it appears to inflate my class.

Look at the error LOG, As soon as I change the orientation (line 5) Android can execute the onActivityCreated () method, but it ends there.

From what I've noticed in my searches, when I change the orientation Android re-creates all the fragments, from the onAttach () method to the onActivityCreated (), then does the same thing with the parent classes, and only after it goes to the following methods (onStart (), etc.).

I would like to try to solve this, because TextWatcher helps me a lot in the App, I did not want to create another solution, since there is already a ...

05-14 02:02:19.635: D/onDestroy(12870): TESTE - TimesFragment
05-14 02:02:19.635: D/onDestroy(12870): TESTE - ChangeHoursFragment
05-14 02:02:19.830: D/onCreateView(12870): TESTE ClockFragment
05-14 02:02:19.830: D/onCreateView(12870): TESTE - TimesFragment
05-14 02:02:19.909: D/onActivityCreated(12870): TESTE - TimesFragment
05-14 02:02:19.987: D/AndroidRuntime(12870): Shutting down VM
05-14 02:02:19.987: W/dalvikvm(12870): threadid=1: thread exiting with uncaught exception (group=0x40e331f8)
05-14 02:02:20.049: E/AndroidRuntime(12870): FATAL EXCEPTION: main
05-14 02:02:20.049: E/AndroidRuntime(12870): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.lft.app/br.lft.app.appViewPager}: android.view.InflateException: Binary XML file line #40: Error inflating class fragment
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1976)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2001)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3371)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.access$700(ActivityThread.java:129)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.os.Looper.loop(Looper.java:137)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.main(ActivityThread.java:4516)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at java.lang.reflect.Method.invokeNative(Native Method)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at java.lang.reflect.Method.invoke(Method.java:511)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at dalvik.system.NativeStart.main(Native Method)
05-14 02:02:20.049: E/AndroidRuntime(12870): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class fragment
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at br.lft.app.AppFragment.onCreateView(AppFragment.java:24)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1135)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.Activity.performStart(Activity.java:4655)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1949)
05-14 02:02:20.049: E/AndroidRuntime(12870):    ... 12 more
05-14 02:02:20.049: E/AndroidRuntime(12870): Caused by: java.lang.NumberFormatException: Invalid int: ""
05-14 02:02:20.049: E/AndroidRuntime(12870):    at java.lang.Integer.invalidInt(Integer.java:138)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at java.lang.Integer.parseInt(Integer.java:359)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at java.lang.Integer.parseInt(Integer.java:332)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at br.lft.app.TimesFragment.calculateExtras(TimesFragment.java:225)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at br.lft.app.TimesFragment$6.onTextChanged(TimesFragment.java:107)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.TextView.sendOnTextChanged(TextView.java:8054)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.TextView.setText(TextView.java:3429)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.TextView.setText(TextView.java:3282)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.EditText.setText(EditText.java:95)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.TextView.setText(TextView.java:3257)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.widget.TextView.onRestoreInstanceState(TextView.java:3127)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.View.dispatchRestoreInstanceState(View.java:10030)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.View.restoreHierarchyState(View.java:10008)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.Fragment.restoreViewState(Fragment.java:425)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:304)
05-14 02:02:20.049: E/AndroidRuntime(12870):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
05-14 02:02:20.049: E/AndroidRuntime(12870):    ... 26 more

Note: I already tried to put all the contents of onCreateView () to onActivityCreated (), but it did not work.

    
asked by anonymous 14.05.2015 / 07:19

1 answer

1

When there is a change of direction the TextWatcher is running, since the text was probably filled and when it spins it is gone. If you notice it is trying to grab an empty string and convert to integer, which causes the application to crash. Just put a Try Catch to handle this situation.

Error:

  

05-14 02: 02: 20.049: E / AndroidRuntime (12870): Caused by: java.lang.NumberFormatException: Invalid int: ""

Solution:

try{
    //TODO tenta converter a string para int
}catch(NumberFormatException e){
    //TODO não conseguiu converter a string para int
}
    
21.05.2015 / 19:04