您正在尋找的Intent的構造函數需要一個上下文,然後是要啓動的類(一個活動)。
從您的視圖類,你應該能夠做到這一點:
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();
}
}
}
現在最有可能您正在創建從Activity類這種觀點所以在這個類,只需創建GameViewInterface的一個實例。當您在Activity中調用onEnterBattlefield()時,請按照我向您展示的意圖調用startActivity。
非常感謝!這正是我需要的。 –