2011-08-16 53 views
0

首先请注意,我是一个noob。Android,如何让这个工作?

我所试图做的事: 呼叫中的其他类中的代码从活动 一旦工作我要开始一个新的活动,并呼吁从那里代码(请告诉我如何多数民众赞成要做的事)

我工作过这个例如:http://stackoverflow.com/questions/7061594/loading-mp3s-into-the-sound-pool-in-android

当我点击按钮,它力眼下

退出:(

我的代码:

class MyApp extends Application { 
    private static MyApp singleton; 

    private static SoundPool mSoundPool; 
    private HashMap<Integer, Integer> soundPoolMap; 

    public static final int A1 = 1; 

    @Override 
    public void onCreate() { // This onCreate is for this class 
     super.onCreate(); 
     singleton = this; 
     mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);// Just an example 
     soundPoolMap = new HashMap<Integer, Integer>(); 

     soundPoolMap.put(A1, mSoundPool.load(MyApp.this, R.raw.a, 1)); 
    } 

    public void playSound(int sound) { 
     AudioManager mgr = (AudioManager)MyApp.this.getSystemService(Context.AUDIO_SERVICE); 
     float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 
     float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
     float volume = streamVolumeCurrent/streamVolumeMax; 

     mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);  
    } 

    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); 

    } 

     public void next_activity(View v) 
     { 
     //MyApp.getInstance().getSoundPool().playSound(1); 
MyApp.getInstance().playSound(1); 

     } 
} 

我的清单文件,如果你需要它:
http://pastebin.com/YHjgGPyd

谢谢!
Ryan

+0

请增加一个堆栈跟踪。 – mibollma

+0

什么是堆栈跟踪?我在哪里/如何添加它? – Ryan

+1

stacktrace ==引起强制关闭的异常的整个输出。在Eclipse中查看LogCat窗口。 – elevine

回答

2

我认为你的错误来自你在你的清单中有两个应用程序标签的事实。这会导致你的第二个应用程序永远不会实例化,所以当你尝试使用它时,你会得到一个NullPointerException异常。

删除第二个和改变你的第一个这样的:

<application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name"> 

虽然这个例子可能是一起工作的良好做法,你真的不应该使用的应用程序级的这个样子。让我引用Android开发人员:

*“通常不需要继承应用程序,在大多数情况下,静态单例可以以更模块化的方式提供相同的功能,如果您的单例需要全局上下文(例如注册广播接收器),那么检索它的函数可以被赋予一个Context,它在第一次构建单例时在内部使用Context.getApplicationContext()。“

http://developer.android.com/intl/de/reference/android/app/Application.html

不仅仅是谷歌就如何使一个漂亮的单,并给它在构造函数中的应用程序上下文:)

编辑:

这里是同一类作为一个单身。最好使用它而不是继续使用Application-class。

public class MySingleton { 

    private MySingleton instance; 

    private static SoundPool mSoundPool; 
    private HashMap<Integer, Integer> soundPoolMap; 

    public static final int A1 = 1; 

    private MySingleton() { 
     mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);// Just an example 
     soundPoolMap = new HashMap<Integer, Integer>(); 
     soundPoolMap.put(A1, mSoundPool.load(MyApp.this, R.raw.a, 1)); 
    } 

    public synchronized MySingleton getInstance() { 
     if(instance == null) { 
      instance = new MySingleton(); 
     } 
     return instance; 
    } 

    public void playSound(int sound) { 
     AudioManager mgr = (AudioManager)MyApp.this.getSystemService(Context.AUDIO_SERVICE); 
     float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); 
     float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
     float volume = streamVolumeCurrent/streamVolumeMax; 

     mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);  
    } 

    public SoundPool getSoundPool() { 
     return mSoundPool; 
    } 
} 
+0

这就是为什么我做了它,并使用两个应用程序标签的原因:http://stackoverflow.com/questions/7061594/loading -mp3s-into-the-sound-pool-in-android如果你知道更好的方法来将我的mp3加载到soundpool并在全球范围内访问它们,我将更愿意抛出任何我已有的代码。现在就给你一个答案...... brb in 3 – Ryan

+0

不行,不工作,它甚至没有加载 – Ryan

+1

真的吗?我用你的清单现在看起来的样子制作了一个pastebin,这是行不通的吗? http://pastebin.com/48a0NtXL – pgsandstrom