调用函数时服务为null

时间:2013-06-04 08:08:37

标签: java android

我有一个函数loadPresetClicked(View v)。在一个示例中,它通过单击按钮进行调用,但我想在没有按钮loadPresetClicked()的情况下调用它。

public void loadPresetClicked() {
    String[] names = new String[10 + model.getPresets().size()];
    for (int i = 0; i < 10; i++) {
        names[i] = eqService.equalizer().getPresetName((short) i);
    }
    for (short i = 0; i < model.getPresets().size(); i++) {
        names[i + 10] = model.getPresets().get(i).getName();
    }

    this.onSaveInstanceState(new Bundle());

}

均衡器模型:

public class EqualizerModel extends Observable {
    private ArrayList<Preset> presets;
    private Scanner inStream;
    private PrintWriter outStream;
    private EqualizerActivity parent;
    private short[] bandLevels;

    public EqualizerModel(EqualizerActivity parent) {
        presets = new ArrayList<Preset>();
        this.parent = parent;
        bandLevels = new short[5];
    }

    public void writePresetFile() {
        File directory = new File("/sdcard/Aurem/");
        directory.mkdirs();
        File outputFile = new File(directory, "presets.txt");
        FileOutputStream outputStream;
        String output = "";
        try {
            outputStream = new FileOutputStream(outputFile);
            outStream = new PrintWriter(outputStream);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        for (int i = 0; i < presets.size(); i++) {
            short index = (short) i;
            Preset preset = presets.get(index);

            output += preset.index() + ",";
            short[] bands = preset.getBands();
            for (int j = 0; j < 5; j++) {
                output += bands[j] + ",";
            }
            output += preset.getName() + "\n";
        }
        outStream.print(output);
        outStream.close();
    }

    public void writeLastStateFile() {
        File directory = new File("/sdcard/Aurem/");
        directory.mkdirs();
        File outputFile = new File(directory, "lastState.txt");
        FileOutputStream outputStream;
        String output = "";
        try {
            outputStream = new FileOutputStream(outputFile);
            outStream = new PrintWriter(outputStream);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        for (short i = 0; i < bandLevels.length; i++) {
            output += bandLevels[i] + "\n";
        }
        outStream.print(output);
        outStream.close();
    }

    public void readLastStateFile() {
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream("/sdcard/Aurem/lastState.txt");
            inStream = new Scanner(inputStream);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        inStream.useDelimiter("\n");
        for (short i = 0; i < 5; i++) {
            bandLevels[i] = Short.parseShort(inStream.nextLine());
        }
        inStream.close();
    }

    public void readPresetFile() {
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream("/sdcard/Aurem/presets.txt");
            inStream = new Scanner(inputStream);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        inStream.useDelimiter("\n");
        while (inStream.hasNext()) {
            String line = inStream.next();
            String[] data = line.split(",");
            short index = Short.parseShort(data[0]);
            short[] bands = new short[5];
            Preset preset = new Preset(index, data[6]);
            for (int i = 1; i < 6; i++) {
                bands[i - 1] = Short.parseShort(data[i]);
            }
            preset.setBands(bands);
            for (int i = 0; i < presets.size(); i++)
                Log.d("Preset", presets.get(i) + "");
            presets.add(index, preset);
        }
        inStream.close();
    }

    public Preset getPreset(short index) {
        return presets.get(index);
    }

    public void setBandLevel(short index, short bandLevel) {
        bandLevels[index] = bandLevel;
        setChanged();
        notifyObservers();
    }

    public short getBandLevel(short index) {
        if (index < 0 || index >= bandLevels.length) {
            return 0;
        }
        return bandLevels[index];
    }

    public void createPreset(String name, short[] bands) {
        int index = presets.size();
        Preset preset = new Preset((short) (index), name);
        preset.setBands(bands);
        presets.add(preset);
    }

    public ArrayList<Preset> getPresets() {
        return presets;
    }

}

均衡器服务:

public class EqualizerService extends Service {

    private final ServiceBinder binder = new ServiceBinder();
    private Equalizer eq;

    @Override
    public void onCreate() {
        super.onCreate();

        eq = new Equalizer(1000000, 0);
        for (short i = 0; i < 5; i++) {
            eq.setBandLevel(i, (short) 0);
        }
        eq.setEnabled(true);
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return binder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public String testPrintout() {

        String output = eq.getProperties().toString() + "\n";
        for (short i = 0; i < 5; i++) {
            output += eq.getBandLevel(i) + " ";
        }
        output += "\n";
        short band = 1;
        short level = 300;
        eq.setBandLevel(band, level);
        for (short i = 0; i < 5; i++) {
            output += eq.getBandLevel(i) + " ";
        }
        output += "\n\n";
        for (short i = 0; i < 10; i++) {
            output += i + "  " + eq.getPresetName(i) + "\n";
        }
        short[] range = eq.getBandLevelRange();
        output += "\n" + "Level Range:  " + range[0] + " to " + range[1] + "\n";

        output += "\nThe Center Frequencies of each band\n";
        for (short i = 0; i < 5; i++) {
            output += i + "  " + eq.getCenterFreq(i) + "\n";
        }
        return "THIS IS A TEST!!!\n\n" + output;

    }

    public Equalizer equalizer() {
        return eq;
    }

    public class ServiceBinder extends Binder {
        EqualizerService getService() {
            return EqualizerService.this;
        }
    }

}

在主要活动中,我启动服务并致电loadPresetClicked(),但会抛出NullPointerException

model = new EqualizerModel(this);
initUI();

File file = new File("/sdcard/Aurem/presets.txt");
if (!file.exists()) {
    model.writePresetFile();
}
model.readPresetFile();

file = new File("/sdcard/Aurem/lastState.txt");
if (!file.exists()) {
    for (short i = 0; i < 5; i++) {
        model.setBandLevel(i, (short) 0);
    }
} else {
    model.readLastStateFile();

}
intent = new Intent(this, EqualizerService.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
isServiceOn = true;
loadPresetClicked();

添加LogCat输出:

06-04 11:11:30.828: E/AndroidRuntime(16106): FATAL EXCEPTION: main
06-04 11:11:30.828: E/AndroidRuntime(16106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.musicplayer.app/com.musicplayer.eq.EqualizerActivity}: java.lang.NullPointerException
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.os.Looper.loop(Looper.java:155)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread.main(ActivityThread.java:5493)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at java.lang.reflect.Method.invokeNative(Native Method)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at java.lang.reflect.Method.invoke(Method.java:511)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at dalvik.system.NativeStart.main(Native Method)
06-04 11:11:30.828: E/AndroidRuntime(16106): Caused by: java.lang.NullPointerException
06-04 11:11:30.828: E/AndroidRuntime(16106):    at com.musicplayer.eq.EqualizerActivity.loadPresetClicked(EqualizerActivity.java:225)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at com.musicplayer.eq.EqualizerActivity.onCreate(EqualizerActivity.java:88)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.Activity.performCreate(Activity.java:5066)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
06-04 11:11:30.828: E/AndroidRuntime(16106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
06-04 11:11:30.828: E/AndroidRuntime(16106):    ... 11 more

0 个答案:

没有答案