Android orientation (PORTRAIT/LANDSCAPE) addTextChangedListener

Asked

Viewed 596 times

1

I’m having 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 are the "editText.addTextChangedListener(new Textwatcher() {...});". If I remove them, the App works normally, but when you place them appears except inflate my class.

Note the error log, As soon as I change the orientation (line 5) Android can run the method onActivityCreated(), but ends there.

From what I could notice in my searches, when I change the orientation Android creates again all Fragments, from the method onAttach() to onActivityCreated(), then does the same thing with the parent classes, so on, and only then 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 and 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 tried to put all onCreateView() content to onActivityCreated(), but it did not help.

1 answer

1

When there is a change of direction the Textwatcher is running, since the text was probably filled and when it rotates it is no longer running. If you observe is trying to take an empty string and convert to integer, which causes the application to break. 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.