Why Android system tries to serialize a View? - android

I have a custom View and sometimes I get reported crashes for:
Caused by: java.io.NotSerializableException: com.myapp.views.MyCustomView
For few FragmentsI have declared a member of this view for easier usage not having to use findViewById all the time. I've also tried to declare member views as transient to prevent this behavior.
Very to hard to post some code, because I have no idea why this is happening, but the basic architecture is following:
Fragment with xml layout
Create MyCustomView which is FrameLayout dynamically and add to that Fragment
Initiate MyCustomView pointer and pass to another View as member for usage
Also I've never got this crash myself, I get the crash report using BugSense crash library.
UPDATE: Somehow this is connected to my one data class which implements Serializable. I decompiled the package and the crash trace also shows is actually a data class:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.myapp.f.y)
And MyCustomViewhas a member of that object. So the Serializable object is being serialized and somehow MyCustomView tries to serialize itself too ?
UPDATE:
Crash count for avg 2k daily users is about 10 per day. Trace showing that when home pressed:
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2302)
system tries to serialize, could there be some threading issue that some operation or animation is still ongoing and thats causing system trying to serialize a View ?
UPDATE:
This seems to happen only with 2.x platform devices.

Related

Android ViewPager2 library throwing Transactiontoolarge exception onpause event

I am getting TransactionTooLarge exception if page size = 50 and I press the home button. I checked the FragmentStateAdapter and found that 'saveState()' method is finalized.
Please help me how to resolve this.
In ViewPager it was overriden by me using below link-
https://medium.com/#mdmasudparvez/android-os-transactiontoolargeexception-on-nougat-solved-3b6e30597345
But no way in View pager2 library.
I had terrible random crash with my fragments.
The one megabyte limit is system wide, so it can crash at much lower thresholds.
I fixed the issue by stopping to use serialized objects and only passing integers into intents and fragment arguments.
Then fragments and activities can get the actual object from a repository using the integer id I gave to it.
It is faster and with insight it is a lot simpler.

Find View id in source code from Crashlytics crash report

I release an app with Crashlytics inside to send crash report, here is report I received:
"Fatal Exception: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.
Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131493375, class android.widget.ListView) with Adapter(class com.nhatnq.app.b.cr)]
at android.widget.ListView.layoutChildren(ListView.java)
at android.widget.AbsListView.onLayout(AbsListView.java)
..."
I want to know which Apdater/ListView got this crash, so I want to trace from these things:
"com.nhatnq.app.b.cr": package name is hidden, I can not know. In my app, I have an "ExpandableContactAdapter extends BaseExpandableListAdapter" in package com.nhatnq.app.contact.ExpandableContactAdapter.java, and some Fragments which include adapter such as com.nhatnq.app.fragment.HistoryFragment$ExpandableHistoryAdapter,
com.nhatnq.app.fragment.MessageFragment$ExpandableMessageAdapter,...
ListView(2131493375): this is ID of ListView. I convert this from dec to hex, then find in my local source code (in R.java), but not found this resource ID. Perhaps ID is encoded by time key when release.
What I need to check here, to find correct ListView/Adapter then I can check? Now I have 5 adapters and really do not know what need to check, as this issue only happen on some phones.
I work in a team, other guy take responsible for releasing app from team source code.
Your release "guy" is probably using Proguard or something similar, so the class name is obsfucated (class com.nhatnq.app.b.cr). This is a procedure makes it harder for someone to reverse engineer your code.
You need to ask him to Retrace (or deobfuscate) the code so you can properly identify the class. He should retain files for doing this, in the case of Proguard, here is a reference:
http://simplyadvanced.net/blog/android-how-to-decode-proguards-obfuscated-code-from-stack-trace/

R.id. values get overwritten

In my Android app I'm creating new WebViews from Java code; however after I create these WebViews my previously working id's get overwritten.
Example snippet:
for (int i=0;i<mywebviewarray.length;i++){
mywebviewarray[i]=new WebView();
}
((Button)findViewById(R.id.mybutton).settext("ok");
If I run this code, I get an exception on the last line:
java.lang.ClassCastException: android.webkit.WebView
It seems to me as if the tables backing the findViewById get overwritten. I tried calling setId in the loop. but it does not help.
How can I resolve this problem?
What is in mywebviewarray. It seems to me like you have something in there that isnt a WebView and you are trying to instantiate it as a WebView.
I ended up not solving this problem, and instead of using a dynamic array of WebViews, I ended up using fixed 3 WebViews. I'm changing their content like a 3-length window over an array of URLs, thus simulating the original concept. (Albeit in a slower way)

Why use parcelable when you can perform the same task using static variables?

i am new in android and java ... i am reading from couples of day about android parceling tutorial for transfer data or variables values from one activity to other or one class to other ... but i am not so understood about that.
can u tell me that is it necessary to use Parcelable for this purpose because same task can also be perform using static key word for variables as string,int or array type then why parcelable pls explain in detail ..
thanks for explanation in advance please provide comparison with example
While technically both approaches will work, there are a couple of flaws.
The first is that the static variable is static. If you have two instances of the same activity, they will both reference the same static object. This is probably not what you want.
Secondly, it's considered bad practice to access global variables. It makes it difficult to see what is going on, is difficult to test and you someone (another class) can modify your data. This creates some horrendous bugs.
By passing the data via a Parcelable object it is very clear what you are doing and you avoid both of these problems.
Note that this advice is not specific to Android, rather to Java and programming in general.
Static references never get garbage collected so you end up creating something called a memory leak.
You are keeping an object in memory that you don't need and it can't be freed up.
If you instantiate enough objects like this you will get an out of memory (oom) exception which will cause the app to crash.

Strange exception in application

I have recently put an application on the Market and I have received until now 7 errors that look like the following:
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.ProgressBar$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x2. Make sure other views do not use the same id.
Can someone tell me how to debug the problem? What is View with id 0x2 - is there a way to find out?
I've received this error under the following conditions - I'm replacing the icon of an item in the actionbar (in my case a throbber for a refresh button), and while it's in that state, I rotate the device. This error arises from it trying to restore the view's saved state and noticing the difference between what's declared and what's serialized.
There should be a stacktrace next to the report which could help you find the exact line of code.

Categories

Resources