【问题标题】:Importing my custom class and calling it's method?导入我的自定义类并调用它的方法?
【发布时间】:2011-04-23 20:32:41
【问题描述】:

我为我的 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 类中,我尝试导入该类,创建它的一个实例,但我想我只是不明白它是如何完成的。有人能指出我正确的方向吗?

【问题讨论】:

  • 我的事情在短短几年内发生了怎样的变化。似乎这个问题仍然很受欢迎,人们也遇到了类似的问题,我想象中的新 Android 开发人员,就像我当时一样。我正在看这个想知道我曾经期望它如何工作。上面代码中明显的一点是上下文从未初始化(实际上也没有任何 hte 字段),该类要么需要构造函数(和非静态字段),要么需要必要的参数(例如上下文、声音池和soundPoolMap) 你在这里看到的直接传递给静态方法。否则,它们将始终为空。

标签: android class import


【解决方案1】:

试试

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

【讨论】:

    【解决方案2】:

    已编辑:来自您的 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;
    }
    }
    

    【讨论】:

    • 我已经尝试了这里指定的所有内容,我最后的手段是“静态”是原因,在上面的建议之后我删除了它们,并且一旦活动尝试,程序仍然会强制关闭调用 s.initSounds();我假设问题是上下文,因为它之前给我带来了问题。你能建议我应该如何正确传递上下文吗?
    • 谢谢,我已经这样做了,但是当我调用 initSounds(); 时程序仍然强制关闭;如果我对此发表评论,它可以正常工作。我不确定它是否相关,但我正在从事的活动是由意图从以前的(父级?)活动中调用的......
    • @Hamid:启动 DDMS 并在强制关闭发生时查找错误消息。
    • 我一直在用logcat做同样的事情,它显示:FATAL EXCEPTION main, RuntimeException "Unable to start Activity" com.mypackage.MyActivity,然后是java.lang.NullPointerException。由 com.mypackage.Sounds.initSounds(Sounds.java:21) 处 com.mypackage.MyActivity.onCreate(MyActivity.java:42) 处的 NullPointerException 引起......在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1069) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
    • 这听起来很奇怪...我不熟悉SoundPool 类,但你确定这行是正确的:soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我对类的这种特殊用法也有疑问。我是 Android 的新手,甚至是使用类的新手,我正在研究 WebInterface。

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

      在 Activity 类中:

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

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 2017-04-19
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2012-09-12
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多