2010-10-11 69 views
5

我为我的Android项目创建了一个名为“声音”的自定义类,我希望能够从我的活动中调用它。是我班的内容如下:导入我的自定义类并调用它的方法?

package com.mypackage; 

import java.util.HashMap; 

import android.content.Context; 
import android.media.SoundPool; 

public class Sounds { 

private static boolean sound = true; 

private static final int FLIP_SOUND = 1; 

private static Context context; 
private static SoundPool soundPool; 
private static HashMap<Integer, Integer> soundPoolMap; 

public static void initSounds() { 
    soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1)); 
} 

public static void playFlip() { 
     soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1); 
} 

public static void setSound(Boolean onOff) { 
    sound = onOff; 
} 
} 

在我的主Activity类我试图导入类,创建它的一个实例,但我想我只是不理解它是如何做。请有人指出我正确的方向吗?

+0

我如何事情在短短几年内就会改变。看起来这个问题仍然很受欢迎,人们也遇到类似的问题,我想像的那些新开发的Android开发人员,就像我当时那样。我正在看着这个想知道我是如何期望它工作的。在上面的代码中,显而易见的事情是上下文永远不会被初始化(实际上也不是hte字段),该类要么需要构造函数(以及非静态字段),要么需要必要的参数(如上下文,soundpool和soundPoolMap)你在这里看到的直接传递给静态方法。否则,它们将始终为空。 – Hamid 2013-08-08 11:52:13

回答

9

编辑:从您的Activity类:

private Sounds s; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
     s = new Sounds(this); 
     s.initSounds(); 
} 

您也可以用构造你的自定义类发送的上下文。

删除静态变量和方法:

public class Sounds { 

private boolean sound = true; 

private int FLIP_SOUND = 1; 

private Context context; 
private SoundPool soundPool; 
private HashMap soundPoolMap; 

public Sounds(Context context){ 
    this.context = context; 
    soundPoolMap = new HashMap(); 
    soundPool = new SoundPool(0, AudioManager.STREAM_MUSIC, 0); 
} 

public void initSounds() { 
    soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1)); 
} 

public void playFlip() { 
    soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1); 
} 

public void setSound(Boolean onOff) { 
    sound = onOff; 
} 
} 
+0

我已经尝试了所有在这里指定的东西,我的最后一招是“静态”的原因,在建议顶部之后,我将它们移除了,并且程序在活动尝试调用s.initSounds()时仍然强制关闭。 ;我假设问题在于上下文,因为它给了我以前的问题。你能建议我应该如何正确传递上下文吗? – Hamid 2010-10-11 15:03:13

+0

@哈米德:我编辑了我的答案。 – Wroclai 2010-10-11 15:14:53

+0

谢谢,我已经做到了这一点,但程序仍然强制关闭,当我打电话给initSounds();如果我对此评论它可以正常工作。我不确定它是否有相关性,但我正在从事的活动是通过意向从以前的(父级?)活动中调用的... – Hamid 2010-10-11 15:23:59

1

尝试

Sounds s = new Sounds(); 
s.initSounds(); 
s.playFlip(); 
s.setSound(true); 
1

所做的类的所有方法是静态的。如果您想按原样使用它们,请拨打Sounds.initSound()等。但是,由于您有类变量,静态方法和变量看起来不合适。从您的成员中删除staticFLIP_SOUND除外),然后尝试创建类的实例并调用正常的方法。

2

我也有关于这类特殊用法的问题。我是Android中的新手,甚至在类的使用中,我正在研究WebInterface。

Shane Oliver的解决方案使用标准类变量为我工作。

在活动类:

Private JavaScriptInterface myJavaScriptInterface; 
myJavaScriptInterface.Toastshow("Hi EveryOne"); 

甚至:

JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this); 

至于类JavaScriptInterface:

public class JavaScriptInterface { 

    Context myContext; 

    //Instanciar o interface e definir o conteudo 
    JavaScriptInterface(Context c) { 
     myContext = c; 
    } 


    public void Toastshow(String toast_msg) 
    { 


     Toast.makeText(myContext, toast_msg, Toast.LENGTH_LONG).show(); 
    } 
} 

希望这有助于...