1

首先,我是一個android新手,所以我的解決方法可以被發現很尷尬,我很樂於提供建議。 我正在嘗試創建一個處理活動之間所有轉換的遊戲管理器對象。我的目的是,在一個活動中,menuOut方法將使用nextActivity參數調用GameManager對象的changeActivity方法,changeActivity將啓動該Activity。我始終得到錯誤,並沒有找到解決方案。Android - 從活動中調用一個普通的對象方法

這裏是我的源代碼: 遊戲管理:

public class GameManager{ 
    public SplashScreen splash = new SplashScreen(); 
    public MainScreen main = new MainScreen(); 
    public LoadingScreen load = new LoadingScreen(); 

    Context tempContext; 

    public GameManager(Context base) { 
     super(); 
     tempContext = base; 
    } 

    public void start(){ 
     createScreens(); 
     Intent intent = new Intent(tempContext, splash.getClass()); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     tempContext.startActivity(intent); 
    } 

    public void createScreens() { 
     //here is the method that i am trying to find a solution 

     ((SplashScreen)splash.getContext()).setGameClass(this); 
     ((MainScreen)main.getContext()).setGameClass(this); 
     ((LoadingScreen)load.getContext()).setGameClass(this); 
    } 

    public void changeMenu(MenuType nextMenu, MenuType previousMenu){ 
     Intent intent2; 
     switch(nextMenu){ 
     case MAIN_SC: 
      tempContext = main.getContext(); 
      intent2.setClass(tempContext, main.getClass()); 
      intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      tempContext.startActivity(intent2); 
     case GAME_LOADING_SC: 
      tempContext = load.getContext(); 
      intent2.setClass(tempContext, load.getClass()); 
      intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      tempContext.startActivity(intent2); 
     default: 
      break; 
     } 
    } 
} 

這裏是閃屏活動:

public class SplashScreen extends Activity { 
    public Context context = this; 
    public GameManager gameman; 
    private static final int SPLASH_DURATION = 4000; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     splash(); 
     menuOut(); 
    } 

    public Context getContext() { 
     return this; 
    } 

    public void splash() { 
     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.HORIZONTAL); 
     ll.setBackgroundResource(R.drawable.game_loop_splash); 

     setContentView(ll); 

     Handler handler = new Handler(); 

     // run a thread after 2 seconds to start the home screen 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       finish(); 
      } 
     }, SPLASH_DURATION); 
    } 

    public void setGameClass(GameManager game){ 
     gameman = game; 
    } 

    private void menuOut(){ 
     gameman.changeMenu(MenuType.GAME_LOADING_SC, MenuType.GAME_SPLASH_SC); 
     this.onDestroy(); 
    } 
} 

我不能返回到遊戲管理和調用changeMenu方法。 我很費勁得到空指針異常。 有什麼想法?

+0

你得到的錯誤是什麼? – codeMagic

+0

我得到這些運行時錯誤:[link](http://tinypic.com/r/11ax5lj/5) –

+0

你正在''createScreens()'中獲得'NPE'。當你創建一個GameManager類的實例時,你不能使用傳入的'Context'嗎? – codeMagic

回答

1

從我讀到的,你試圖實現一個單身模式。有兩種方法,我建議這樣做在Android上:

  1. 擴展Application類,清單中的註冊類,並使用getApplication()你們的活動來訪問它:

    // In MyApplicationSubclass.java: 
    public final class MyApplicationSubclass extends Application { 
        /* ... */ 
        public void myMethod() { 
        // insert some code here 
        } 
        /* ... */ 
    } 
    
    // From your Activity: 
    ((MyApplicationSubclass) this.getApplication()).myMethod(); 
    
  2. 使用「正常」java單例模式,例如使用private構造函數,並在您的GameManager類中保留一個靜態實例(這是Android文檔推薦的方式,但我個人更喜歡在邏輯上綁定到應用程序時的第一種方式)。


另外,如果你只使用你的核心類做靜態的東西,你可以標記爲static其所有的方法,並直接訪問它們。將Context對象作爲參數傳輸到這些方法,並且您應該能夠在沒有任何靜態變量的情況下啓動活動(有時難以在Android中正確實現,因爲您的虛擬機可能會不時重新啓動)。

+0

其實我不能得到你的應用程序解決方案。你能詳細解釋一下嗎? –

+0

@TalatTekbudak希望這個例子有所幫助嗎?我也鏈接了Android文檔。 – dst

+0

不要擔心應用程序解決方案太多。基本上,您擴展了** Application **類以將持久狀態添加到它,然後通過清單安排您的子類而不是默認的** Application **類。我偶爾會這樣做,但單身模式也起作用。看到我的其他職位的單身人士的更多信息:http://stackoverflow.com/questions/13670862/setting-a-value-in-one-class-and-retrieving-from-another-class-in-java-android/ 13673178#13673178 –

相關問題