2011-08-16 30 views
1

我正在關閉另一個例子(在這裏找到Loading MP3s into the Sound Pool in Android)。但由於某種原因,我結束了兩個onCreate()方法,如果我刪除一個我收到錯誤。Android中的兩個onCreate()方法?

我想將MP3加載到Soundpool中一次,然後在其他活動中調用它們。

我的代碼:

class MyApp extends Application { 
    private static MyApp singleton; 

    private static SoundPool mSoundPool; 

    public onCreate() { // ##### Deleting this gives me errors ###, not deleting it gives me 1 error. 
     super.onCreate(); 
     singleton = this; 
     mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);// Just an example 
    } 

    public static MyApp getInstance() { 
     return singleton; 
    } 

    public SoundPool getSoundPool() { 
     return mSoundPool; 
    } 
} 


public class TestAndroidGlobalsActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 
+1

因此,您在應用程序類中有1個'onCreate',在'Activity'中有另外1個,這沒關係。問題是爲什麼你要在1個文件中粘貼Application和Activity。 – ernazm

+0

我認爲這是如何做到這裏:http://stackoverflow.com/questions/7061594/loading-mp3s-into-the-sound-pool-in-android如果我把它放在一個單獨的文件,我該怎麼稱呼它? – Ryan

回答

3

您的代碼只有每一個類的方法onCreate。它們用於不同的目的,並在每個類由OS創建時調用。

在創建應用程序時調用您的MyApp.onCreate,並在您的MyApp類中設置應用程序範圍的狀態。

當創建Activity時調用TestAndroidGlobalsActivity.onCreate,併爲該特定活動設置狀態以初始化其UI。

+0

第一個onCreate告訴我「缺少方法的重新類型」,我該怎麼做? (我是新手) – Ryan

+0

杜,忘了「空白」! – Ryan

1

你一定不要混淆Application.onCreate,創建您的應用程序時調用,Activity.onCreate,叫你每次活動被調用時(其可以在一個應用程序執行期間發生幾次)

此外,你應該每個重寫方法上方都有@Override註釋。

+0

謝謝!這就說得通了! – Ryan

相關問題