gpt4 book ai didi

java - 启动 Android Activity 会给出 IllegalStateException/InvocationTargetException

转载 作者:行者123 更新时间:2023-11-29 04:39:49 25 4
gpt4 key购买 nike

我试图让我的 Android 应用程序更加面向对象,并创建一个类 (DigitHandler),它选择从特定类型的输入调用哪个 Activity 。当用户单击另一个名为 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);
}

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

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 block 我在 InvocationTarget 中发现了实际的异常:“从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是您想要的吗?”

所以它听起来就像我可以添加一个语句 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);并让问题消失。那个警告和 this thread 使这听起来像个坏主意,但我不确定具体原因。

最佳答案

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

所以只需替换

getApplicationContext() 

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.

关于java - 启动 Android Activity 会给出 IllegalStateException/InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com