我试图使我的Android应用程序更加面向对象,并创建一个类(DigitHandler),该类从某种输入类型中选择要调用的活动。当用户单击另一个名为InputCodeActivity的类中的按钮时,将调用DigitHandler。

DigitHandler调用以下launchGame方法:

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


但是最后一行(开始活动)给我一个错误。 (我知道这是最后一行,因为如果我注释掉那一行,我不会得到错误。)我真的不知道是什么原因导致了错误。这是错误和堆栈跟踪:

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


知道我在做什么错吗?

编辑:上下文是getApplicationContext(),它是通过当前Activity中的okButtonClick()方法传递的,如下所示:

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


DigitHandler的构造函数将查找上下文(在DigitHandler中变为ctx,并传递给launchGame())和字符串theNumber。

编辑:使用try-catch块,我发现了InvocationTarget内的实际异常:
“从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是您想要的吗?”

因此,听起来好像我只能添加一条语句intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);并解决问题。该警告和this thread听起来似乎不是一个好主意,但我不确定为什么。

最佳答案

您的onButtonClick中有一个小问题。您正在传递getApplicationContext()作为上下文的引用,但实际上您需要在此处传递当前活动引用作为上下文。

所以只要更换

getApplicationContext()




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



  注意:每当您需要执行任何需要电流的操作时
  上下文,那么您必须将当前活动作为contex传递。例如对于
  打开一个新活动需要当前的上下文引用
  活动。

10-06 16:18