2013-01-08 64 views
1

我正在爲Android做一個回合制RPG遊戲。我有一個擴展視圖的類,我需要啓動另一個類也擴展視圖。第一類是玩家在地圖周圍走動,第二類是戰鬥屏幕。我試圖讓它工作,但我得到這個錯誤。是否可以在擴展視圖的類中使用意圖?

The constructor Intent(GameView, Class<BattleView>) is undefined 

我曾經使用過intents之前沒有任何問題,但我從來沒有試圖在擴展視圖的類中使用intent。我想這就是我遇到問題的原因。 是否可以在擴展視圖的類中使用意圖?

任何想法?

回答

2

您正在尋找的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。

+0

非常感謝!這正是我需要的。 –

相關問題