I'm trying to start a service in a different app that's installed on the device. I get a NullPointerException when using startService
public void notConnected(){
Log.i(TAG,"no connection... reconnecting.");
Intent reset = new Intent("com.famoco.intent.action.TOGGLE_DATA");
Log.i(TAG,"calling " + reset.getAction());
if(reset.getAction().equals("com.famoco.intent.action.TOGGLE_DATA"))
{
startService(reset);
}
else
{
Log.i(TAG,"couldn't start service");
}
}
and this is error log
E/AndroidRuntime: FATAL EXCEPTION: IntentService[MyAppCommService]
Process: com.myapppackage.MyApp, PID: 8583
java.lang.NullPointerException
at android.content.ContextWrapper.startService(ContextWrapper.java:494)
at com.myapppackage.MyApplocation.activity.MyAppActivity.notConnected(MyAppActivity.java:591)
at com.myapppackage.MyApplocation.api.MyAppApi.submitClock(MyAppApi.java:228)
at com.myapppackage.MyApplocation.service.MyAppCommService.submitTags(MyAppCommService.java:52)
at com.myapppackage.MyApplocation.service.MyAppCommService.onHandleIntent(MyAppCommService.java:98)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.os.HandlerThread.run(HandlerThread.java:61)
Turns out my context returned null, I fixed it by getting the context in onCreate and calling myContext.startservice.
You are starting a new Intent. And looking at that Google example, looks like you use the wrong method to start it up.
- you use:
startService(reset);
- try this one:
startActivity(intent);
But that starts an Activity. And you want an service. I don't think you can do that unless you are the developer of that other app. In general, Android Apps don't have access each others code & resources.
Fixed it by returning context in the onCreate() and then calling context.startService()
Related
I am using camera kit library for my app. When I get the picture call back, I convert the byte array to bitmap and try to pass it to the next activity like this:
ab_capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cameraView.captureImage(new CameraKitEventCallback<CameraKitImage>() {
#Override
public void callback(CameraKitImage cameraKitImage) {
byte[] jpeg = cameraKitImage.getJpeg();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
Intent previewIntent = new Intent(ImageActivity.this, PreviewActivity.class);
previewIntent.putExtra("cam_image", bitmap);
startActivity(previewIntent); // implicit
finish();
}
});
}
});
but after clicking the button, app crashes and I get this error message:
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 3686928)
02-28 01:03:06.183 9579-9999/base.android.com.thumbsapp E/AndroidRuntime: FATAL EXCEPTION: CameraViewWorker
Process: base.android.com.thumbsapp, PID: 9579
java.lang.RuntimeException: Failure from system
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4226)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4185)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
at android.app.Activity.startActivity(Activity.java:4509)
at android.app.Activity.startActivity(Activity.java:4477)
at base.android.com.thumbsapp.UI.Activities.ImageActivity$1$1.callback(ImageActivity.java:83)
at base.android.com.thumbsapp.UI.Activities.ImageActivity$1$1.callback(ImageActivity.java:75)
at com.wonderkiln.camerakit.CameraView$4.imageCaptured(CameraView.java:471)
at com.wonderkiln.camerakit.Camera1$5.onPictureTaken(Camera1.java:427)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1361)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: android.os.TransactionTooLargeException: data parcel size 3686928 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:622)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3197)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4226)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4185)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
at android.app.Activity.startActivity(Activity.java:4509)
at android.app.Activity.startActivity(Activity.java:4477)
at base.android.com.thumbsapp.UI.Activities.ImageActivity$1$1.callback(ImageActivity.java:83)
at base.android.com.thumbsapp.UI.Activities.ImageActivity$1$1.callback(ImageActivity.java:75)
at com.wonderkiln.camerakit.CameraView$4.imageCaptured(CameraView.java:471)
at com.wonderkiln.camerakit.Camera1$5.onPictureTaken(Camera1.java:427)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1361)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.os.HandlerThread.run(HandlerThread.java:61)
This error only occurs on a Nougat device. When I run on Lollipop, there is no error but it doesn't start the preview activity.
I need help understanding and resolving this error and also the problem with going to the next activity. Thanks.
You should never pass this big data using intents. It will crash on most devices and is not well designed.
First store file to disk then put its path to intent
This error only occurs on a Nougat device
It will happen on the vast majority of Android devices.
I need help understanding and resolving this error
Your Intent is too large, due to the large Bitmap extra.
The best solution is to not have two activities here. Have one activity with a changing UI, such as via the use of fragments.
Alternatively, carefully pass the Bitmap via a static field, making sure to set that field to null ASAP, so you do not have a long-term memory leak.
My activity 'used' the put a custom (serializable) class in the bundle when starting a Service. But couple version back I had remove it (and delete the class) and directly put primitives in the bundle.
And now... my ACRA log is getting completely hammered by this error of a crash in the onStartCommand when on the line the bundle is read.
java.lang.RuntimeException: Unable to start service com.xxx.MyService#db4c909 with Intent { act=ACTION_START flg=0x4 cmp=com.xxx/.MyService (has extras) }: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.xxx.MyBundle)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3045)
at android.app.ActivityThread.access$2200(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = com.xxx.MyBundle)
at android.os.Parcel.readSerializable(Parcel.java:2491)
at android.os.Parcel.readValue(Parcel.java:2294)
at android.os.Parcel.readArrayMapInternal(Parcel.java:2592)
at android.os.BaseBundle.unparcel(BaseBundle.java:221)
at android.os.BaseBundle.get(BaseBundle.java:281)
at com.xxx.MyService.onStartCommand(MyService.java:190)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)
....
... 14 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.xxx.MyBundle" on path: DexPathList[[zip file "/data/app/com.xxx-2/base.apk"],nativeLibraryDirectories=[/data/app/com.xxx-2/lib/arm, /data/app/com.xxx-2/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
I don't get what I'm getting error knowing that the com.xxx.MyBundle doesn't even exist anymore in the project!
Note that Im seeing this error in my crash logs. I cannot reproduce this locally probably because I have uninstalled and reinstall the app many times and this it probably fixes the problem.
The issue here is that there thousands of people which the app is probably crashing and cannot just tell to everyone "uninstall- reinstall". I'd like to find the cause of this and possibily fix it with a new update without the user having to manually uninstall the app and reinstall..
EDIT
This is the history of event
v1: I was putting a Serializable class in the bundle when starting the service
(I had forgotten to set the serialVersionUID) and I was seeing a lot of serialization error when starting the service like version mismatch when desializing.. typical error when forgetting the serialVersionUID
v2: I have added serialVersionUID=-1 in my Serializable class
(No dice, in the log I was still seeing these serialization is now saying found version={random number} but was -1....
v3: I gave up and deleted com.xx.MyBundle and created a com.xx.MyBundle2
(no Dice.. and this is when I started to see in the log that ClassNotFoundException)
v4: I Delete com.xx.MyBundle2 and instead of putting the serializable class in the bundle I put directly my primitives
So you see.. it is not a question of service running or not.. Even when my users update from v3 to v4 I would have expect this error to be gone but no... I still see the ClassNotFoundExcpetionn in v4
As I guess from your question, you might have started a Service with a previous version of your application which was removed lately along with the classes used. Its causing the ClassNotFoundException.
So, in this specific case, you might consider detecting the application update and on application update you need to check if the Service is running in background which needs to be stopped.
To check for your newer version of application is installed you'll receive a broadcast event called ACTION_MY_PACKAGE_REPLACED and then you stop the running Service.
To check if your desired Service is already running, you might consider having a checker like this.
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
And then call it using
if(isMyServiceRunning(MyService.class))
stopMyService();
Update
I think I've understood your problem correctly and thought of a solution above already. I guess, the Service you started already in your v1 or v2 is running and expecting the bundle of serialized class there. So you need to stop the service and start it again to make it follow the current behaviour which you're expecting.
Very rarely getting:
Fatal Exception: java.lang.IllegalArgumentException: Unknown URL content://com.example.provider/info
at android.content.ContentResolver.insert(ContentResolver.java:1252)
Fatal Exception: java.lang.IllegalArgumentException: Unknown authority com.example.provider
at android.content.ContentResolver.applyBatch(ContentResolver.java:1247)
Emphasis on rarely. Generally work fine without issue, so the authorities is set up fine, but this is showing up every once in a while for no reason. Are there reasons why the ContentResolver may not be able to find a ContentProvider (i.e. if not set up yet)?
I've had the rare IllegalArgumentException with Unknown URIs issue when I was doing ContentResolver operations in the custom Application object.
For example, I was trying to delete items in my content provider in the application onCreate method which would very occasionally crash:
public class CustomApplication extends Application {
#Override
public void onCreate() {
//..
context.getContentResolver().delete(ReminderEntry.getContentURI(), null, null, null, null);
//..
}
}
Which would sometimes render the following crash:
Fatal Exception: java.lang.RuntimeException: Unable to create application com.myapp.CustomApplication: java.lang.IllegalArgumentException: Unknown URL content://com.myapp.db.CustomContentProvider/reminder
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6431)
at android.app.ActivityThread.access$1800(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.lang.IllegalArgumentException: Unknown URL content://com.myapp.db.CustomContentProvider/reminder
at android.content.ContentResolver.delete(ContentResolver.java:1376)
at com.myapp.ReminderEntryDao.delete(Unknown Source)
at com.myapp.CustomApplication.onCreate(Unknown Source)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1037)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6428)
at android.app.ActivityThread.access$1800(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I also saw similar behaviour with a BOOT_COMPLETE receiver. I had about 70 reported crashes with this exception (mostly Infinix devices ~43%, hardly any Samsung Devices) of about 200 000 monthly active users.
I moved this into a background scheduled job and haven't seen the crash since. I was only ever able to reproduce this issue once on a Nexus device that I used but never again.
I suspect perhaps sometimes on some versions of Android on some devices the Application/BOOT_COMPLETE Receiver initializes before the ContentProvider is fully initialized and therefore when it tries to access it, it is not properly set up yet.
There are a couple of stackoverflow posts that do state exactly what is created first and how the OS should behave:
Is the Application class guaranteed to be instantiated before a defined boot receiver is called
But like I said, I've seen otherwise and moving operations out of the classes into background schedulers seems to fix the problem (perhaps it is just because it takes a bit longer to get setup). Hopefully my experience will help you.
Edit: I used the evernote job dispatcher and deferred my ContentResolver operations to the job if required. (but I would assume that deferring the content provider operation to any kind of background processing might fix it as it had a bit more time to get setup - these are just my suspicions of course).
class DeleteRemindersJob extends Job {
#NonNull
#Override
protected Result onRunJob(final Params params) {
cursor = getContext().getContentResolver().delete(ReminderEntry.getContentURI(), null, null, null, null);
//..
return Result.SUCCESS;
}
}
In the "Crashes and ANRs" of the Google Play Developer console I've got such a report:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.res.Resources.getAssets()' on a null object reference
at android.app.LoadedApk.getAssets(LoadedApk.java:590)
at android.app.LoadedApk.makeApplication(LoadedApk.java:646)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5088)
at android.app.ActivityThread.access$1600(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
at android.os.Handler.dispatchMessage(Handler.java:102)
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:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
The device that has this problem is Galaxy S4 and runs Android 5.0
What it can be - there is not a single line from my code, why does it fail?
Thanks a lot!
I've got this in my console too.
It seems this occurs when users start the app when it's currently being updated or just after that.
A possible workaround would be to check if getResources returns null when the application start, and kill it if it does:
public class DevToolsApplication extends Application {
private static final String TAG = "DevToolsApplication";
#Override
public void onCreate() {
super.onCreate();
AppLogger.i(TAG, "app start...");
checkAppReplacingState();
}
private void checkAppReplacingState() {
if (getResources() == null) {
AppLogger.w(TAG, "app is replacing...kill");
Process.killProcess(Process.myPid());
}
}
}
See this for more information
https://issuetracker.google.com/issues/36972466
Make sure that anywhere you call getAssets(), you call it as:
getApplicationContext().getAssets()
It would appear as if you are calling getAssets() in a class that does not have the application context available, hence the fact that it is null.
I've got the same exception while added the below overlay in the AndroidManifest.xml file. Even after removing the Overlay code, landed into the above exception.
<overlay android:targetPackage="com.android.systemui"
android:priority="1"/>
Just close the Android Studio completely. Open the project again. Clean and Build the exception got cleared.
I'm developing a watchface for Android Wear using the WatchFace API (extending CanvasWatchFaceService).
I've used the code from here to build a ticker that run code every second.
I'm experiencing the following problem. Every now and then the service crashes with this exception. I can't understand where it comes from, if you have any lead I'll post additional code.
01-06 11:22:00.247 12965-12965/com.my.package E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.my.package, PID: 12965
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.PowerManager$WakeLock.acquire()' on a null object reference
at android.support.wearable.watchface.WatchFaceService$Engine.onCommand(WatchFaceService.java:201)
at android.service.wallpaper.WallpaperService$Engine.doCommand(WallpaperService.java:977)
at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1191)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
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)
After this crash the watchface stops working and to make it start again I have to choose another watchface, then reselect mine, so it's a total show stopper!
Props to David for the lead on super. calls - it turns out that was the problem. Specifically, the CanvasWatchFaceService.Engine.onCreate method needs to call through to its ancestor, as such:
private class Engine extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
// your engine initialization code here
}
// other watch face engine code
}
Without the super.onCreate(holder); call, my watch face would crash within minutes; with it, it happily ran overnight.
As an aside, this is something missing from the Android developer documentation; specifically, the Training page for Building a Watch Face Service doesn't include this ancestor call in its code sample.
The code you linked lacks some important parts, that are dotted. I.e. have you defined the update rate?
private static final long INTERACTIVE_UPDATE_RATE_MS = TimeUnit.SECONDS.toMillis(1);
Fully implemented it works well. See AnalogWatchFaceService in code samples: Wearable/Watchface face.