2

我正在研究另一个示例(在此处找到Loading MP3s into the Sound Pool in 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);
    }
}
4

2 回答 2

3

您的代码每个类只有一个onCreate方法。它们有不同的用途,并在操作系统创建每个类时调用。

您在创建应用程序并在您的类中MyApp.onCreate设置应用程序范围的状态时调用。MyApp

TestAndroidGlobalsActivity.onCreate创建 Activity 并设置该特定 Activity 的状态并初始化其 UI 时调用您。

于 2011-08-16T14:29:13.477 回答
1

您不能混淆 Application.onCreate(在创建应用程序时调用)和 Activity.onCreate(在每次调用活动时调用)(在一个应用程序执行期间可能会发生多次)

此外,您应该在每个覆盖方法上方都有 @Override 注释。

于 2011-08-16T14:30:46.817 回答