Android 是否可以在扩展视图的类中使用意图?
我正在为Android做一个基于回合的RPG游戏。我有一个扩展视图的类,我需要启动另一个扩展视图的类。第一类是玩家在地图上走动的地方,第二类是战斗屏幕。我试图让它工作,但我得到了这个错误Android 是否可以在扩展视图的类中使用意图?,android,class,android-intent,view,Android,Class,Android Intent,View,我正在为Android做一个基于回合的RPG游戏。我有一个扩展视图的类,我需要启动另一个扩展视图的类。第一类是玩家在地图上走动的地方,第二类是战斗屏幕。我试图让它工作,但我得到了这个错误 The constructor Intent(GameView, Class<BattleView>) is undefined 构造函数意图(GameView,类)未定义 我以前使用过意图,没有任何问题,但我从未尝试在扩展视图的类中使用意图。我想这就是我遇到问题的原因。 是否可以在扩展视图的类
The constructor Intent(GameView, Class<BattleView>) is undefined
构造函数意图(GameView,类)未定义
我以前使用过意图,没有任何问题,但我从未尝试在扩展视图的类中使用意图。我想这就是我遇到问题的原因。
是否可以在扩展视图的类中使用意图
有什么想法吗?您正在寻找的构造函数将获取一个上下文,然后是您想要启动的类(一个活动)
从视图类中,您应该能够执行以下操作:
Intent intentToLaunch = new Intent(getContext(), BattleView.class);
这将正确创建您的意图,但除非您将活动传递到视图,否则您将无法从视图启动活动,这是一个非常糟糕的主意。实际上,这是一个糟糕的设计,因为您的视图不应该启动其他活动。相反,视图应该调用该视图的创建者将响应的接口
它可能看起来像这样:
public class GameView extends View {
public interface GameViewInterface {
void onEnterBattlefield();
}
private GameViewInterface mGameViewInterface;
public GameView(Context context, GameViewInterface gameViewCallbacks) {
super(context);
mGameViewInterface = gameViewCallbacks;
}
//I have no idea where you are determining that they've entered the battlefield but lets pretend it's in the draw method
@Override
public void draw(Canvas canvas) {
if (theyEnteredTheBattlefield) {
mGameViewInterface.onEnterBattlefield();
}
}
}
现在,您很可能是从活动类创建此视图,因此在该类中,只需创建GameViewInterface的实例。当您在活动中收到对OneInterbattle()的调用时,请按照我向您展示的意图调用startActivity。非常感谢!这正是我需要的。