konmik / nucleus

Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
MIT License
1.98k stars 253 forks source link

InstantiationException for Presenter #112

Closed null1099 closed 7 years ago

null1099 commented 8 years ago

I have the two following classes (simplified):

MyActivity.java:

@RequiresPresenter(MyPresenter.class)
public class MyActivity extends NucleusAppCompatActivity<MyPresenter> {
...
}

MyPresenter.java:

public class MyPresenter extends Presenter<MyActivity> {
...
}

when launching MyActivity, I get the following RunTimeException:

Caused by: java.lang.InstantiationException: 
java.lang.Class<MyPresenter> has no zero argument constructor                                                                
at java.lang.Class.newInstance(Native Method)                                                               
at nucleus.factory.ReflectionPresenterFactory.createPresenter(ReflectionPresenterFactory.java:39)                                                                
at nucleus.view.PresenterLifecycleDelegate.getPresenter(PresenterLifecycleDelegate.java:57)                                                               
at nucleus.view.PresenterLifecycleDelegate.onResume(PresenterLifecycleDelegate.java:96)                                                                 
at nucleus.view.NucleusAppCompatActivity.onResume(NucleusAppCompatActivity.java:70)                                                                 
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)                                                              
at android.app.Activity.performResume(Activity.java:6327)                                                                
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)                                                                 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)                                                         
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)                                                                 
at android.os.Handler.dispatchMessage(Handler.java:102)                                                                 
at android.os.Looper.loop(Looper.java:148)                                                                 
at android.app.ActivityThread.main(ActivityThread.java:5422)

This doesn't change even when I add the required zero argument constructor to MyPresenter (besides, I've never seen this in the examples). Is there anything I missing?

This happens for Nucleus v3.0.1 as well as v4.0.0

konmik commented 8 years ago

Hi, it looks like you do not have a default constructor in you presenter :) (It is something said by the exception)

konmik commented 8 years ago

Ups, sorry. Now I did read it till the end. Did you try to completely clean/rebuild you project?

null1099 commented 7 years ago

Hi, clean/rebuild has no effect, still getting the exception.

TWiStErRob commented 7 years ago

@null1099 Do you have proguard, or some APT tool in the build chain? Try adding this to your activity class to see what the runtime sees:

static {
    try {
        Log.d("MyPresenter", "Constructors:");
        for (Constructor<?> constructor : MyPresenter.class.getDeclaredConstructors()) {
            Log.d("MyPresenter", constructor.toString());
        }
    } catch(Throwable t) {
        Log.e("MyPresenter", "Error enumerating constructors", t);
    }
}

(it needs to be in the activity because the presenter class is not initialized until first access, which would be inside a successful newInstance after the sanity checks are done)

I noticed that null is using Android 23 or 24 (because newInstance became native on those platforms). I tried an empty project with just these two classes and the only time I got the same exception is when I added a constructor with argument(s). I tried a lot of other things, like failed static initializer, visibility, abstract, but all of them gave a different error message.