How to find out what's causing BadTokenExceptions - android

I'm working on an Android application that has multiple activities and services. Some of the activities are defined in 3rd party libraries that I'm importing into my project and the problem is that on some devices (specially in Samsung Galaxy Tabs) my app keeps crashing when switching from one activity to the previous via clicks on the back button.
I did a LogCat and found this to be the cause of the crashes:
android.view.WindowManager$BadTokenException: Unable to add window -- token
android.os.BinderProxy#351c808e is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
but the interesting thing is that none of my application's classes appears on the stack trace. I searched for similar problems on StackOverflow but every other report I found that had a stack trace of a BadTokenException, the respective application's code always appeared in the stack trace.
Does anyone know what are the common causes for this problem and/or the best way to troubleshoot it? I noticed that this problem happens more on Samsung devices so maybe it's an actual bug on those devices.

Usually BadTokenExceptions occur when your activity attempts to create a new Window before its onAttachToWindow() method is called (or after its onDetachFromWindow() method is called). It could very well be that the third party libraries you are using are buggy and do not ensure that this requirement is met.

Related

Identify Context.startForegroundService() source in stack traces?

I get this exception on occasion:
Context.startForegroundService() did not then call
Service.startForeground() android.app.ActivityThread.main
The fun thing is I have no idea from the stack traces when or where startForegroundService is being called. I know it's not in my code, but it could be in one of the 3rd party libs I'm using. Unfortunately, the stack traces are all but useless. Of course, I can't reproduce it reliably, that would be too easy! Does anyone have any tips/ideas on how to track down the origin?
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1790)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
If it is crashing "in the lab", you could look at Logcat to see what service was started ~10 seconds before the crash.
Beyond that, use the "Merged Manifest" tab in Android Studio's manifest editor and see what services are showing up in your manifest (plus, what libraries contributed those manifest entries). That at least narrows down which libraries might be the culprit. If any of those are open source, you could then poke through the source code to see if they seem to call startForegroundService().

Exception while accessing calendar on android device

Getting an exception while launching Calendar on android device.
Logcat:
`
FATAL EXCEPTION: main
Process: com.google.android.calendar, PID: 7209
java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:48)
at com.android.calendar.calendarlist.CalendarListUtils.processCursor(CalendarListUtils.java:155)
at com.android.calendar.calendarlist.SelectCalendarsAdapter.swapCursor(SelectCalendarsAdapter.java:160)
at com.android.calendar.calendarlist.DrawerFragment.onLoadFinished(DrawerFragment.java:244)
at com.android.calendar.calendarlist.DrawerFragment.onLoadFinished(DrawerFragment.java:55)
at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
at android.content.Loader.deliverResult(Loader.java:144)
at android.content.CursorLoader.deliverResult(CursorLoader.java:109)
at android.content.CursorLoader.deliverResult(CursorLoader.java:97)
at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:265)
at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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)
Force finishing activity com.google.android.calendar/com.android.calendar.AllInOneCalendarActivity
`
Did anyone get this issue? Any help would be appreciated.
#Krishna,
Problem could be solved by setting --> App and clearing cache and data from the calendar app and do a force stop of the calendar app.
Also, there is one more app which is running for calendar called "Calendar storage", which could be found in Setting --> App and on the top menu click on Show System.
On this screen all system Apps will be visible. Click on Calendar storage and follow all the steps for clearing cache and data and force stop the app.
Now start the Calendar App, It will start without any issue.
This is an off-topic question. StackOverflow is intended for programming questions.
Anyway, just for the sake of answering, I think your Calendar app hasn't been granted permissions sufficient permissions.
Head over to Settings --> Apps --> Calendar.
Click on Permissions and enable all in it.
Now reboot the phone and try. It should work by now.
If it still doesn't work, try clearing it's data & cache and try again.

How to hide other apps in Android?

I am going to write a project that can hide another apps. I do research and find some useful code and it work well to hide my app. You can see my code below:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
And the problem is that, I don't know how can I hide another apps. I have tried to do lot of research but I still cannot find any solution. So you can look at my code below (but it is not working to hide another apps, but to hide my own is working well):
PackageManager p = getPackageManager();
ComponentName com = new ComponentName("com.example.pro", "com.example.pro.classname");
p.setComponentEnabledSetting(com, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
I know that the first parameter for ComponentName is a package name of the app, and second parameter is a main activity name of the app that I want to hide. But it always force close every time i changed both of this parameters to another value. But if I change the first parameter to package name of my app and second parameter to the main activity name of my app, it work well like the first code I show above.
And I have some log that i got when my app force close below:
04-01 22:56:32.884 13339-13339/com.example.pro D/AndroidRuntime: Shutting down VM 04-01 22:56:32.884 13339-13339/com.example.pro E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pro, PID: 13339
java.lang.SecurityException: Permission Denial: attempt to change component state from pid=13339, uid=10613, package uid=10464
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSetting(IPackageManager.java:4073)
at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:1839)
at com.example.pro.MainActivity$1.onClick(MainActivity.java:142)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20931)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
So the problem is that, how can I hide another apps ? Any other code/method/suggest/example is fine. And i'd appreciate your help. Thanks.
I don't know how can I hide another apps
Fortunately, you cannot do that from an ordinary SDK app, for obvious security reasons. Ransomware authors would love the ability to do what you are proposing.
There are some hooks for doing what amounts to "hide another apps" in the device owner APIs IIRC, but they require the app be a device owner app, and that requires special work at the time the phone or tablet is first powered on after being purchased.
You may be able to accomplish what you want on a rooted device, and you certainly can do it with your own custom ROM. You could also create a home screen that "hides" apps by simply not showing them in its own launcher.

Why do I get "Permission Denial: not allowed to send broadcast com.motorola.intent.SYSTEM_DIALOG_POPUP"?

Background
We have a very large and very popular app with quite a few permissions.
The problem
It seems that on Motorola devices only (XT1254,XT1585,XT1565), with Android 5.1 and 5.1.1, we get crashes of this kind:
Fatal Exception: java.lang.SecurityException: Permission Denial: not allowed to send
broadcast com.motorola.intent.SYSTEM_DIALOG_POPUP from pid=15407, uid=10164
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2864)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1510)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:382)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:382)
at android.app.Dialog$2.run(Dialog.java:316)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
The above statistics were collected using Crashlytics.
Not sure if it is because of this, but one of the app's permission is "SYSTEM_ALERT_WINDOW", to be able to show content on top of other apps.
Thing is, this error seems to be because of Motorola's code itself, and I can't find anything about it over the Internet.
What I've tried
Apart from searching the Internet about this exception, I've also tried to find how to contact Motorola itself, including finding a developer forum, but I've failed in doing so.
I've also tried posting about this on Android's group:
https://code.google.com/p/android/issues/detail?id=201631
The question
What can be done in this case?
Why does it occur? Is it because of the permission? Is there a workaround ?
Hi actually me and my team mates faced the exact same problem when we were building an application which shows notes on top of other applications.
After researching for great lengths we found out that "SYSTEM_ALERT_WINDOW" permissions was made a signature permission and was causing problems by not letting the pop up appear from Lollipop 5.1
As a work around to it we need to explicitly ask the user to grant overlay permission for your app by doing this.
public void checkOverlayPermission(){
if(!Settings.canDrawOverlays(this)){
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:"+getPackageName()));
startActivityForResult(intent, ApplicationConstants.OVERLAY_PERMISSION);
}
}
Hope this helps

java.lang.RuntimeException from Adobe AIR app

I have an app on Amazon that sometimes (less than 1% of installations) crashes, which is reflected in Crash Reports. Obviously, I don't have access to the actual devices where the app crashed. Below is one of the crash reports. Can anything be done to identify the cause of the crashes?
java.lang.RuntimeException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2672)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
at android.app.ActivityThread.access$2100(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException
at android.app.Activity.missingDialog(Activity.java:2636)
at android.app.Activity.dismissDialog(Activity.java:2621)
at com.amazon.android.framework.prompt.Prompt.dismissDialog(Unknown Source)
at com.amazon.android.framework.prompt.Prompt.dismiss(Unknown Source)
at com.amazon.android.framework.prompt.PromptManagerImpl.finish(Unknown Source)
at com.amazon.android.framework.prompt.PromptManagerImpl.a(Unknown Source)
at com.amazon.android.framework.prompt.i.a(Unknown Source)
at com.amazon.android.n.e.a(Unknown Source)
at com.amazon.android.n.b.a(Unknown Source)
at com.amazon.android.framework.context.d.a(Unknown Source)
at com.amazon.android.framework.context.d.onDestroy(Unknown Source)
at com.amazon.android.Kiwi.onDestroy(Unknown Source)
at air.com.sierravistasoftware.SightWordsSB1.AppEntry.onDestroy(AppEntry.java)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2659)
... 11 more
You are having problems dismissing a dialog. Maybe the problem comes when the dialog is started, the device orientation is changed and the activity is recreated without the dialog. In this case, if you try to dismiss the dialog, the app will crash.
Try replicating it to see if this is the problem.
You may prefer using DialogFragment to avoid this problem or simply catch the exception and move on, as you got what you were looking for (your dialog dismissed).
UPDATE:
Your error log states that it's trying to dismiss a missing dialog when the activity is being destroyed. It could be an issue in the com.amazon.android.framework source code, and maybe the users get prompted for something before the crash.
You could try to replicate situations when your Activity is destroyed.
Following our chat via the comments, I can't think in anything else without seeing the code, so I encourage you to use a bug tracking system to help you find under what conditions is your app crashing.
Try ACRA:
ACRA is a library enabling Android Application to automatically post their
crash reports to a GoogleDoc form. It is targetted to android
applications developers to help them get data from their applications
when they crash or behave erroneously.

Categories

Resources