1

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.

DigitHandler calls this launchGame method:

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);
}

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:

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
-------------------------------

Any idea what I'm doing wrong?

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

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

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

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?"

So it sounds like I could just add a statement intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and make the problem go away. That warning and this thread make that sound like a bad idea, but I'm not sure exactly why.

Community
  • 1
  • 1
Displaced Hoser
  • 871
  • 3
  • 13
  • 35
  • How you passing the reference of context variable in this class. It must be the activity instance reference. Can you show us how you passing the context reference from the activity class in it? – EEJ Sep 24 '16 at 22:57
  • What is the context? Also, what is the code for your new activity? Seems like you are getting exception while opening the new activity – amiekuser Sep 25 '16 at 18:14

2 Answers2

2

There is a small issue in your onButtonClick. You are passing getApplicationContext() as a reference for your context but actually you need to pass the current activity reference as a context here.

So just replace

getApplicationContext() 

with

Activity.this (where Activity = Name of your current Activity class) 

Note: Whenever you need to perform any operation requiring current context then you must pass current activity as contex. e.g. for opening a new activity required the context reference of current activity.

EEJ
  • 678
  • 5
  • 12
1

The launchGame() method is not on the stack trace posted. The problem seems to be with the View$1.onClick callback.

java.lang.reflect.InvocationTargetException is a wrapper of an exception occurred while calling methods with reflection. Try to catch InvocationTargetException within onClick() and use getCause() to find out the wrapped exception.

Worth to look at: What could cause java.lang.reflect.InvocationTargetException?

Community
  • 1
  • 1
Onik
  • 19,396
  • 14
  • 68
  • 91