Android strange ClassCastException - android

A few days ago we started to receive some crash reports from users using our app, but the stracktrace is really strange. It happens only on Samsung G900f (S5) and I can't reproduce it and decide to post a question here.
The stacktrace shows this:
Caused by java.lang.ClassCastException: android.support.design.widget.CollapsingToolbarLayout cannot be cast to android.support.design.widget.CollapsingToolbarLayout
at com.packageName.profile.ProfileFragment.onViewCreated(SourceFile:423)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1086)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1248)
at android.support.v4.app.BackStackRecord.run(SourceFile:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1613)
at android.support.v4.app.FragmentController.execPendingActions(SourceFile:330)
at android.support.v4.app.FragmentActivity.onStart(SourceFile:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
at android.app.Activity.performStart(Activity.java:6258)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
It is a Fragment which contains CollapsingToolbarLayout with initialization and use all correct, but not sure why this happens.
We got some other ClassCastExceptions with different classes from the same device. For example:
Caused by java.lang.ClassCastException:
com.packageName.model.VideoMeta cannot be cast to
com.packageName.model.VideoMeta
where VideoMeta is a class which implements Parcelable and this exception occurs in class Video which implements Parcelable too:
private Video(Parcel in) {
id = in.readLong();
postId = in.readString();
url = in.readString();
thumb = in.readString();
meta = (VideoMeta) in.readValue(VideoMeta.class.getClassLoader());
}
But that's not the end...
Caused by java.lang.ClassCastException:
android.support.design.widget.AppBarLayout$LayoutParams cannot be cast to
android.support.design.widget.AppBarLayout$LayoutParams
I have no idea what can cause this exception and why it's happening, that's why posting it here.
Thanks in advance for any kind of help or suggestions!

Related

ArrayIndexOutOfBounds LifecycleRegistry.popParentState

I came across an exception on which I do not have a clue on how to fix it. Here is the stacktrace:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1
at java.util.ArrayList.remove(ArrayList.java:480)
at android.arch.lifecycle.LifecycleRegistry.popParentState(LifecycleRegistry.java:194)
at android.arch.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:293)
at android.arch.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:332)
at android.arch.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:137)
at android.arch.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:123)
at android.arch.lifecycle.ReportFragment.dispatch(ReportFragment.java:121)
at android.arch.lifecycle.ReportFragment.onStart(ReportFragment.java:81)
at android.app.Fragment.performStart(Fragment.java:2378)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1027)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:2048)
at android.app.FragmentController.dispatchStart(FragmentController.java:188)
at android.app.Activity.performStart(Activity.java:6978)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2937)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
I'm using android.arch.lifecycle:extensions:1.1.1.
UPDATE
I got more informations.
This is happening when I start an activity for result from within a Fragment by calling Fragment.startActivityForResult(). This Fragment is in "strange" place. Here it is the structure of its holding Activity
- HoldingActivity
-- HoldingFragment
--- FragmentPagerAdapter
---- MyFragment // This is where I call the startActivityForResult
Another point that might help is that every level of the HoldingActivity is using ViewDataBinding.setLifecycleOwner(this) and they all have a ViewModel attached to it.
The started Avtivity is hosting a Fragment and they also both use ViewDataBinding.setLifecycleOwner(this) and have a ViewModel.
Let me know if anything else could help resolve this issue, thanks.

Sometimes get android.content.res.Resources$NotFoundException

We're debugging a crash in a Cordova app that seems to happen on launch / foregrounding, after the app was backgrounded for a moderate amount of time and has been to some extent killed by the OS.
At the time of the crash the app is attempting to show a native dialog fragment for Fingerprint Authentication. For some reason in this case the resources for this fragment cannot be found.
It's been reported for Android 6 and 7, on older and newer devices.
java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.koho/ca.koho.Koho}: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:5440)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1437)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2887)
at android.content.res.Resources.getLayout(Resources.java:1251)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.cordova.plugin.android.fingerprintauth.FingerprintAuthenticationDialogFragment.onCreateView(FingerprintAuthenticationDialogFragment.java:85)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1130)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1953)
at android.app.FragmentController.dispatchActivityCreated(FragmentController.java:152)
at android.app.Activity.performCreateCommon(Activity.java:6279)
at android.app.Activity.performCreate(Activity.java:6287)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
... 9 more
Here's the problematic bit of code, from the DialogFragment's onCreateView:
int fingerprint_dialog_container_id = getResources()
.getIdentifier("fingerprint_dialog_container", "layout",
FingerprintAuth.packageName);
View v = inflater.inflate(fingerprint_dialog_container_id, container, false);
When the crash happens, getIdentifier(...) is for some reason returning 0. Most of the time the actual identifier is returned.
I have not found a way to reproduce the crash, and advice to enable developer options like 'Don't keep activities' and 'Background process limit' had did not help.
Thanks for any help.

illegal state exception from startactivity

Hi guys I am getting the following exception
ERR-5e1e9c7f83ba94b45017220ad632c|StackTrace:java.lang.IllegalStateException: Fragment Gallery{d6a81c0} not attached to Activity
--------- Stack trace ---------
android.support.v4.app.Fragment.startActivity(Fragment.java:914)
mypackage.Gallery$openReceiptImage.onPostExecute(Gallery.java:1604)
mypackage.Gallery$openReceiptImage.onPostExecute(Gallery.java:1424)
android.os.AsyncTask.finish(AsyncTask.java:651)
android.os.AsyncTask.access$500(AsyncTask.java:180)
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
android.os.Handler.dispatchMessage(Handler.java:102)
android.os.Looper.loop(Looper.java:148) android.app.ActivityThread.main(ActivityThread.java:7325) java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
------------------------------- --------- Cause --------- -------------------------------
My code is as follows
Intent fullimage=new Intent(mContext, Preview.class);
startActivity(fullimage);
Here mContext is Context and I am calling this from Fragment. Preview is Activity. I beleieve these informations are enough. And also I am not getting this issue always.
Can anyone tell me what could be the reason for this issue.
Thanks in Advance.
Issue is fixed
if(isAdded){ }
Is used to check whether that fragment is attached to Activity or not

inflater.inflate within onCreateView of fragment is throwing Exception

The following lines within a Fragment are throwing an exception for which I am unable to find the root cause.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_player_pane, container, false);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rootView.getLayoutParams();
// set width height
params.height = (getArguments().getInt(HEIGHT));
params.width = (getArguments().getInt(WIDTH));
fragmentHeight = (getArguments().getInt(HEIGHT));
fragmentWidth = (getArguments().getInt(WIDTH));
// substitute parameters for left,top, right, bottom, (All 4 not necessary)
params.setMargins((getArguments().getInt(LEFT_MARGIN)),
(getArguments().getInt(TOP_MARGIN)),
(getArguments().getInt(RIGHT_MARGIN)),
(getArguments().getInt(BOTTOM_MARGIN)));
youTubePlayerNecessary = getArguments().getBoolean(REQUIRE_YOUTUBE);
rotate = getArguments().getBoolean(REQUIRE_ROTATION);
muteAudio = getArguments().getBoolean(MUTE_AUDIO);
rootView.setLayoutParams(params);
return rootView;
}
Exception:
11-02 11:36:04.966 3440-3452/com.blynq.app.multipaneplayer E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:127)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.startChromiumLocked(WebViewChromiumFactoryProvider.java:16107)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.ensureChromiumStartedLocked(WebViewChromiumFactoryProvider.java:333)
at com.android.webview.chromium.WebViewChromiumFactoryProvider.startYourEngines(WebViewChromiumFactoryProvider.java:427)
at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:162)
at android.webkit.WebView.<init>(WebView.java:554)
at android.webkit.WebView.<init>(WebView.java:489)
at android.webkit.WebView.<init>(WebView.java:472)
at android.webkit.WebView.<init>(WebView.java:459)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.blynq.app.fragments.PlayerPane.onCreateView(PlayerPane.java:181)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
I am currently running app in debug mode to check all possible memory leaks.
if (BuildConfig.DEBUG) {
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.penaltyDeath()
.build());
}
All tags in manifest file are properly closed too. I am unable to figure out which resource was left open without being handled. How do I resolve this ?
P.S. I have referred to thread before asking the question, which is a very generic question offering no new necessary insight for me. This question is very specific to the details mentioned above, here I am seeking help to find which resource is causing the leak..
Somebody raised an issue for this here: https://code.google.com/p/android/issues/detail?id=226751
I've been having the same issue, and it looks like it's a bug in API < 25. Try your program in an emulator with API 25 and see if it works. If it works there, then it is most likely the same problem and you are out of luck until is able to fix the issue.
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog()
.penaltyDeath().build());
For more Information Follow this Url
Error Solve Url

Retrifit and simpleXMLConverter

I've been trying to use Retrofit with an API which works with XML. I also notice that retrofit have an XML Converter called SimpleXmlConverter here:
https://github.com/mobile-professionals/retrofit-simplexmlconverter
But when I tried to use it, got this error:
FATAL EXCEPTION: Thread-14844
retrofit.RetrofitError: retrofit.converter.ConversionException: java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForType cannot be cast to java.lang.Class
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:409)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:284)
at com.aaaa.common.$Proxy0.configs(Native Method)
at com.aaaa.common.DemoAPI.getContributos(DemoAPI.java:47)
at com.aaaa.MainActivity$1.run(MainActivity.java:27)
at java.lang.Thread.run(Thread.java:841)
Caused by: retrofit.converter.ConversionException: java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForType cannot be cast to java.lang.Class
at retrofit.converter.SimpleXMLConverter.fromBody(SimpleXMLConverter.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:394)
            
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:284)
            at com.aaaa.common.$Proxy0.configs(Native Method)
            at com.aaaa.common.DemoAPI.getContributos(DemoAPI.java:47)
            at com.aaaa.MainActivity$1.run(MainActivity.java:27)
            at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.ClassCastException: org.apache.harmony.luni.lang.reflect.ImplForType cannot be cast to java.lang.Class
at retrofit.converter.SimpleXMLConverter.fromBody(SimpleXMLConverter.java:36)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:394)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:284)
            at com.aaaa.common.$Proxy0.configs(Native Method)
            at com.aaaa.common.DemoAPI.getContributos(DemoAPI.java:47)
            at com.aaaa.MainActivity$1.run(MainActivity.java:27)
            at java.lang.Thread.run(Thread.java:841)
I'm making a sample App so I'm just trying to be simple with my code:
RestAdapter restAdapter = new RestAdapter.Builder()
//.setEndpoint(API_URL)
.setServer(API_URL)
.setConverter(new SimpleXMLConverter())
.build();
And
static class Config {
String name;
int firstInt;
int secondInt;
}
interface Demo {
#GET("/{user}/api/config")
List<Config> configs (
#Path("user") String owner
);
}
You're not using the "SimpleXmlConverter" class that belongs to the retrofit-simplexmlconverter library. Instead you're using the "SimpleXMLConverter" class (see uppercase "XML") that is part of retrofit itself since a while. I'd advise you to go with the official one as this is unit tested and under more active development.
I created the retrofit-simplexmlconverter lib when there was no xml converter for retrofit yet.
Unfortunately I don't see the reason for your problem at first sight. The implementation of the converter classes should be similar. So concerning your problem I think it shouldn't make a difference which one you use. If I were you I'd step in with a debugger to get a clue about what's causing the ClassCastException.
Got it!!
Looks like Inner Classes can't be instantiated in the same way than normal classes by reflect, they need to be instantiated by a Constructor, so.. SimpleXMLconverter not works with inner classes like mine:
static class Config {
String name;
int firstInt;
int secondInt;
}
I just made a new folder "Models" with that classes and works perfect! ;)
Really thanks to #Sebastian Engel for teach me and show me how it to solve it.

Categories

Resources