Skip to content

error NotificationIntentService has no zero argument constructor #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andrew19881123 opened this issue Sep 14, 2016 · 14 comments
Closed

Comments

@andrew19881123
Copy link

I saw the note on the nativescript blog

IntentService’s implementation needs to use a constructor that takes no arguments, but that currently is not possible through Java.

but how do I solve the problem to make this app works?

System.err: java.lang.RuntimeException: Unable to instantiate service com.tns.notifications.NotificationIntentService: java.lang.InstantiationException: java.lang.Class<com.tns.notifications.NotificationIntentService> has no zero argument constructor
09-14 21:49:13.297 12889 12889 W System.err:    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2862)
09-14 21:49:13.297 12889 12889 W System.err:    at android.app.ActivityThread.-wrap4(ActivityThread.java)
09-14 21:49:13.297 12889 12889 W System.err:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
09-14 21:49:13.300 12889 12889 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 21:49:13.300 12889 12889 W System.err:    at android.os.Looper.loop(Looper.java:148)
09-14 21:49:13.300 12889 12889 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:5417)
09-14 21:49:13.301 12889 12889 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
09-14 21:49:13.301 12889 12889 W System.err:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-14 21:49:13.301 12889 12889 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-14 21:49:13.301 12889 12889 W System.err: Caused by: java.lang.InstantiationException: java.lang.Class<com.tns.notifications.NotificationIntentService> has no zero argument constructor
09-14 21:49:13.301 12889 12889 W System.err:    at java.lang.Class.newInstance(Native Method)
09-14 21:49:13.301 12889 12889 W System.err:    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2859)
09-14 21:49:13.301 12889 12889 W System.err:    ... 8 more

@WinMinTun
Copy link

@andrew19881123 @slavchev I m having the same problem and cannot move passed. Pls help!!

@petekanev
Copy link
Contributor

petekanev commented Sep 21, 2016

@andrew19881123 @WinMinTun

With version 2.2.0 of the android runtime you can open the generated .java file and remove the extra piece of code yourself. With the recent update to the binding generator in 2.3.0 .java files are no longer generated and that can't be done.

Take a look at https://github.com/NativeScript/sample-android-background-services/blob/master/platforms/android/src/main/java/com/tns/notifications/NotificationIntentService.java I left it there purposely for visibility

@WinMinTun
Copy link

WinMinTun commented Sep 21, 2016

@Pip3r4o

Yes I see it there. But the problem is my edited code gets overwritten whentns run android.

  1. Generated .java file cannot be edited - overwritten when build and run. I just need to add
    public NotificationIntentService() { this("NotificationIntentService"); } in java. How can I do that to the JS?
  2. And also another problem is that after platform removed, can no longer re-generate .java files. I have already built and run a number of times and still not re-generated.

@petekanev
Copy link
Contributor

@WinMinTun regarding 1) - the lack of empty constructor is JVM's fault, but we normally rewrite generated classes. You may have to disable the binding generator after having generated the java class and editing it. To do that - open platforms/android/build.gradle and comment out setProperties.finalizedBy("asbg:generateBindings")
2) - if you are using 2.2 .java should be generated in src/java, in 2.3 the runtime works with dexes that can not be edited.

@WinMinTun
Copy link

@Pip3r4o Thanks a lot for the details. It works now for 2.2.

@ivanwitzke
Copy link

ivanwitzke commented Dec 5, 2016

Any solution for this on 2.3+ ?
I'm using "tns-core-modules 2.4.1" and "tns-android 2.4.1" and I need to implement a background sync service for the app I'm developing. (Must work even when the app is closed...)

@samuelt1
Copy link

samuelt1 commented Dec 29, 2016

i am having the same problem. I also noticed that this sample project has a few extra files that my project does not have.

platforms/android/src/main/java/com/tns/broadcastreceivers
platforms/android/src/main/java/com/tns/notifications
platforms/android/src/main/java/com/tns/activities

did you manually add these files?
are these the location of the files that @WinMinTun edited to make it work?
are these what fixed the no argument constructor?

I downloaded the code from this repository, and it works fine, but if I remove platforms then i get the no zero argument constructor error, so i think there is something i am not being told here.
what else do i need to add?
I added these files to my project, and it still does not work.

@petekanev
Copy link
Contributor

Project was cleaned up, and a fix applied in the form of a plugin. Feel free to test out the updated sample, and let me know how it works for you. Thanks!

@pacear10
Copy link

Today I am running the project for background services

Https://github.com/NativeScript/sample-android-background-services

But still throwing the same error, is there something that should be done to clone the project to take the changes that made the project to stop displaying the error message?
Thank you.
An uncaught Exception occurred on "main" thread. java.lang.RuntimeException: Unable to instantiate service com.tns.notifications.NotificationIntentService: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor at android.app.ActivityThread.handleCreateService(ActivityThread.java:2641) at android.app.ActivityThread.access$1600(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5227) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.InstantiationException: can't instantiate class com.tns.notifications.NotificationIntentService; no empty constructor at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1319) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2638) ... 10 more

@petekanev
Copy link
Contributor

petekanev commented Jan 24, 2017

@pacear10 From a freshly cloned project, it should be as simple as adding the android platform and running the project, the necessary dependencies should be downloaded, installed, then you'll be good to go.

@pacear10
Copy link

pacear10 commented Jan 25, 2017

Thanks for replying, clone the project, install the android platform, the app starts, but when the button is pressed the error still appears.

@petekanev
Copy link
Contributor

@pacear10 just cloned it (master branch), did npm install followed by tns run android and everything worked perfectly. I am unable to reproduce the crash you are experiencing. Ensure the com.pip3r4o.android.app.IntentService is being extended inside app/notifications/NotificationIntentService

@sam0829
Copy link

sam0829 commented Mar 4, 2017

@Pip3r4o is it possible to generate Java code with 2.5.0 runtime? if not, how can I tell {N} explicitly to use 2.2.0 runtime?

@petekanev
Copy link
Contributor

@sam0829 version 3.0 will bring back generated Java code, if you wish to use a specific runtime, you can install it with tns platform add android@2.2, If adding the 0-params constructor is the only reason that you want to use 2.2, then you can instead npm install nativescript-android-utils, and extend from com.pip3r4o.android.app.IntentService instead of android.app.IntentService

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants