导入我的自定义类并调用它的方法? [英] Importing my custom class and calling it's method?

查看:111
本文介绍了导入我的自定义类并调用它的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的Android项目创建了一个名为Sounds的自定义类,我想从我的活动中调用它。我的类的内容如下:

  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上下文上下文;
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类中,我尝试导入类,创建它的一个实例,但我想我只是不知道如何做。

解决方案

已编辑:从您的活动 class:

  private Sounds s; 

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

您还可以将构造函数的上下文发送到自定义类。 p>

删除静态变量和方法:

  public class Sounds {

private boolean sound = true;

private int FLIP_SOUND = 1;

private上下文上下文;
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;
}
}


I have created a custom class for my Android project called "Sounds" I want to be able to call it from my activity. The contents of my class are as follows:

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

In my main Activity class I have tried importing the class, creating an instance of it but I guess I'm just not understanding how it's done. Can anybody point me in the right direction please?

解决方案

Edited: From your Activity class:

private Sounds s;

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

You might also send the context with the constructor to your custom class.

Remove the static variables and methods:

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

这篇关于导入我的自定义类并调用它的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆