Starting Android activity gives IllegalStateException / InvocationTargetException
<p>I am trying to make my Android app a bit more object-oriented, and create a class (DigitHandler) that selects which activity to call from a certain type of input. DigitHandler is called when the user clicks a button in another class called InputCodeActivity. </p>

<p>DigitHandler calls this launchGame method:</p>

<pre><code>private void launchGame()
{
    Intent intent;

    switch(digits[0])
    {
        case 0:
            // Set random game from among the first three.
            int randNum = (int)(Math.random() * 3) + 1;
            digits[0] = randNum;
            // Not sure whether that will work or just have it continue to 1.
            // Not that important at the moment, since random game isn't in the specs.
        case 1:
            intent = new Intent(ctx, SimonActivity.class);
            break;
        case 2:
            intent = new Intent(ctx, SquareActivity.class);
            break;
        case 3:
            intent = new Intent(ctx, PegsActivity.class);
            break;
        case 4:
            intent = new Intent(ctx, PipesActivity.class);
            break;
        default:
            intent = new Intent(ctx, LightsActivity.class);
    }

    String difficulty = "";

    switch(digits[2])
    {
        case 1:
            difficulty = "easy";
            break;
        case 2:
            difficulty = "medium";
            break;
        case 3:
            difficulty = "hard";
            break;
    }

    intent.putExtra(GameActivity.DIFFICULTY, difficulty);

    // intent.putExtra(GameActivity.ADDITIONAL_PARAMS, barcodeData.additionalParams);

    ctx.startActivity(intent);
}
</code></pre>

<p>But the last line (starting the activity) gives me an error. (I know it is the last line because if I comment that line out I do not get the error.) I really can't tell what's causing the error. Here is the error and stack trace:</p>

<pre><code>java.lang.IllegalStateException: Could not execute method of the activity

--------- Stack trace ---------

    android.view.View$1.onClick(View.java:3841)
    android.view.View.performClick(View.java:4456)
    android.view.View$PerformClick.run(View.java:18465)
    android.os.Handler.handleCallback(Handler.java:733)
    android.os.Handler.dispatchMessage(Handler.java:95)
    android.os.Looper.loop(Looper.java:136)
    android.app.ActivityThread.main(ActivityThread.java:5086)
    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:515)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    dalvik.system.NativeStart.main(Native Method)
-------------------------------

--------- Cause ---------

java.lang.reflect.InvocationTargetException

    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:515)
    android.view.View$1.onClick(View.java:3836)
    android.view.View.performClick(View.java:4456)
    android.view.View$PerformClick.run(View.java:18465)
    android.os.Handler.handleCallback(Handler.java:733)
    android.os.Handler.dispatchMessage(Handler.java:95)
    android.os.Looper.loop(Looper.java:136)
    android.app.ActivityThread.main(ActivityThread.java:5086)
    java.lang.reflect.Method.invokeNative(Native Method)
    java.lang.reflect.Method.invoke(Method.java:515)
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    dalvik.system.NativeStart.main(Native Method)
-------------------------------

--------- Device ---------

Brand: motorola
Device: condor_cdma
Model: XT830C
Id: KXC21.5-40
Product: condor_tracfone
-------------------------------

--------- Firmware ---------

SDK: 19
Release: 4.4.4
Incremental: 35
-------------------------------
</code></pre>

<p>Any idea what I'm doing wrong?</p>

<p>EDIT: Context is getApplicationContext(), passed from the okButtonClick() method in the current Activity, which looks like this:</p>

<pre><code>public void okButtonClick(View view)
{
    theNumber = numberPad.getText().toString();
    Log.e("Recording the number", "Number is"+theNumber);
    DigitHandler dh = new DigitHandler(getApplicationContext(), theNumber);
}
</code></pre>

<p>The constructor for DigitHandler looks for a context (which becomes ctx in the DigitHandler and is passed to launchGame()) and a string, theNumber.</p>

<p>EDIT: Using a try-catch block I found the actual exception inside the InvocationTarget: 
"Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?"</p>

<p>So it <em>sounds</em> like I could just add a statement intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and make the problem go away. That warning and <a href="https://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context">this thread</a> make that sound like a bad idea, but I'm not sure exactly why.</p>