10

I cannot instantiate a sub activity. In the logcat I see this line:

01-22 15:14:38.906: DEBUG/dalvikvm(411): newInstance failed: no <init>()

This is the line in dalvik that generates that logcat.

/*
 * public T newInstance() throws InstantiationException, IllegalAccessException
 *
 * Create a new instance of this class.
 */
static void Dalvik_java_lang_Class_newInstance(const u4* args, JValue* pResult)
...
    /* find the "nullary" constructor */
    init = dvmFindDirectMethodByDescriptor(clazz, "<init>", "()V");
    if (init == NULL) {
        /* common cause: secret "this" arg on non-static inner class ctor */
        LOGD("newInstance failed: no <init>()\n");
        dvmThrowExceptionWithClassMessage("Ljava/lang/InstantiationException;",
            clazz->descriptor);
        RETURN_VOID();
    }

Here is the action I take to activate the activity in a timer handler.

// move on to Activation
// ePNSplash is this activity a splash screen

Intent i = new Intent (ePNSplash.this, Activation.class);
startActivity (i);

The activity that I am trying to start is 2 extensions above Activity

Here is the first extension

public abstract class AndroidScreen extends Activity {
    ....

public AndroidScreen (String title, AndroidScreen parent, AndroidScreen main)
{
    super ();

    myGlobals = Globals.getGlobals ();

    myGlobals.myLogger.logString("AndroidScreen: 001");

    myParent = parent;
    myMainScreen = main;
    myTitle = title;
}

This is only the constructor, which seems to be the part that has the problem. Here is the 2nd extension and the class i am trying to instantiate.

public class Activation extends AndroidScreen {

public Activation (String title, AndroidScreen parent, AndroidScreen main)
{
    super (title, parent, main);
}

I am absolutely confused, I have a constructor, I make sure I call my super constructors, what could possibly be wrong?

Thank you

Julian

Bodger
  • 1,202
  • 2
  • 14
  • 22

1 Answers1

20

dalvikvm's looking for a zero-argument constructor (that's what they mean by "nullary", as in "binary" for 2 arguments, "unary" for 1 argument, it's "nullary" for 0 arguments).

in the snippet you've shown, you only have a three-argument constructor. that's no good: you'll be instantiated with no arguments, so you need a zero-argument constructor.

Elliott Hughes
  • 4,517
  • 2
  • 20
  • 21
  • 2
    My issue was an IntentService which the abstract class does not define a default constructor, only a constructor with an argument (String name). As a result, I was confused by the compiler on implementing a default constructor and left it off. When I added it as MyIntentService() { super(null); } it all worked. This was so counter intuitive -- thanks for the q&a on this exception. – mobibob Dec 28 '10 at 12:51
  • I could'nt believe my eyes when this worked!!! I instantiated and pre-inflated fragments and a zero arg was added when I read this and it worked. Whish I could upvote this even MORE! – Jonathan Natie Klopper Apr 17 '12 at 06:51
  • Yes, the documentation is not very clear on this. For IntentActivity, you need to implement a no-argument constructor, that calls super("threadname"). "threadname" can be anything, it's only used for debugging. And apparently, as you've discovered, null is a valid value. – Edward Falk Nov 09 '12 at 15:34