Service not starting-ссылка на нулевой объект

Я попытался преобразовать одну часть моего приложения в службу, потому что она должна работать в фоновом режиме. Теперь кнопка должна запускаться и останавливаться при нажатии:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aim_start);
    tThis = this;
    calc.init();
    status = (TextView) findViewById(R.id.statusView);
    output = (TextView) findViewById(R.id.lbl_output);
    final Intent intent = new Intent(this, AIMService.class);
    startButton = (Button) findViewById(R.id.start_aim);
    startButton.setOnClickListener(new View.OnClickListener (){
        public void onClick(View v) {
            if (!func) {
                func = true;
                startButton.setText("Stop AIM");
                status.setText("AIM-Started");
                tThis.startService(intent); //I read somewhere that it would need a 
                                            //context, but it didn't work
            } else {
                func = false;
                startButton.setText("Start AIM");
                tThis.stopService(intent);
            }
        }
    });

Теперь, однако, я получаю следующую ошибку:

FATAL EXCEPTION: main
       Process: com.protonmail.fabian.schneider.aim, PID: 6081
       java.lang.RuntimeException: Unable to start service com.protonmail.fabian.schneider.aim.AIMService@1dacca22 with Intent { cmp=com.protonmail.fabian.schneider.aim/.AIMService }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference
       at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3336)
       at android.app.ActivityThread.access00(ActivityThread.java:177)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1530)
        t android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5912)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.app.IntentService$ServiceHandler.obtainMessage()' on a null object reference

Служба объявлена в манифесте.
манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    package="com.protonmail.fabian.schneider.aim">
    <uses-permission android_name="android.permission.INTERNET" />
    <application
        android_allowBackup="true"
        android_icon="@mipmap/ic_launcher"
        android_label="@string/app_name"
        android_supportsRtl="true"
        android_theme="@style/AppTheme">
        <activity android_name=".AIM_start"
                    android_label="AIM"
                    android:configChanges = "orientation"
                    android_screenOrientation="portrait">
            <intent-filter>
                <action android_name="android.intent.action.MAIN" />

                <category android_name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android_name=".AIMService" />
    </application>

</manifest>

услуга:

публичный класс AIMService расширяет IntentService {

private SoundPool soundPool;
private AudioManager audioManager;
private static final int max_streams = 1;
private static final int streamType = AudioManager.STREAM_MUSIC;
boolean loaded;
public static String strengthArr[] = new String[4];

static {
    strengthArr[0] = "90,110,90,110";
    strengthArr[1] = "50,89,111,150";
    strengthArr[2] = "0,49,151,200";
    strengthArr[3] = "-50,-1,201,-250";
}

static String downURL = "http://services.swpc.noaa.gov/text/goes-magnetometer-primary.txt";
static String errPatt = "-1.00e+05";

private float volume;

private int strengthSound[] = new int[5];

@Override
public void onCreate(){

    System.out.println("started audio initialization");
    //AUDIO
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);
    float maxVolumeIndex = (float) audioManager.getStreamMaxVolume(streamType);
    this.volume = currentVolumeIndex/maxVolumeIndex;
    //this.setVolumeControlStream(streamType);


    if(Build.VERSION.SDK_INT >= 21) {
        AudioAttributes audioAttrib = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        SoundPool.Builder builder = new SoundPool.Builder();
        builder.setAudioAttributes(audioAttrib).setMaxStreams(max_streams);
        this.soundPool = builder.build();
    } else{
        this.soundPool = new SoundPool(max_streams, AudioManager.STREAM_MUSIC, 0);
    }
    //Sound pool load complete
    this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            loaded = true;
            System.out.println("finished loading resources");
            //publishProgress("finished loading resources");
            //SoundPool.Builder(new SoundPool.Builder()).play(strengthSound[0],volume,volume,1,0,1f);
            /*MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.strength0);
            MediaPlayer mediaPlayer2 = MediaPlayer.create()
            mediaPlayer.start();*/
        }
    });
    System.out.println("starting tone init");
    this.strengthSound[0] = this.soundPool.load(this, R.raw.strength0,1);
    this.strengthSound[1] = this.soundPool.load(this, R.raw.strength1,1);
    this.strengthSound[2] = this.soundPool.load(this, R.raw.strength2,1);
    this.strengthSound[3] = this.soundPool.load(this, R.raw.strength3,1);
    this.strengthSound[4] = this.soundPool.load(this, R.raw.strength4,1);
    System.out.println("fin tone init");
}


public AIMService(){
    super("AIMService");
}
@Override
protected void onHandleIntent (Intent intent) {
    //WORK

    while (true) {

        System.out.println("before publishProgress Update");
        //publishProgress("Calculation Started");

        AIMService.dataImport dI;
        try {
            dI = new AIMService.dataImport(downURL);
            String calcData;
            calcData = dI.download();

            System.out.println(calcData);
            AIMService.preAnalysis pA;
            pA = new AIMService.preAnalysis(calcData);
            boolean dateOk;
            dateOk = pA.preanalyse();
            if (dateOk) {
                System.out.println("Data Date is okay");
                //call last line downloader
                dI = new AIMService.dataImport(downURL);
                String lastLine;
                lastLine = dI.downloadLast();
                if (!lastLine.contains(errPatt)) {
                    //data is ok
                    System.out.println("Current data is okay");
                    System.out.println("Current data: " + lastLine);
                    System.out.println("Starting analysis");

                    AIMService.analyse an = new AIMService.analyse(lastLine);
                    int strength;
                    strength = an.analyseData();
                    if (strength != -1) {
                        System.out.println("Strength: " + strength);
                        //outputText(Integer.toString(strength));
                        publishProgress(Integer.toString(strength));
                        //return Integer.toString(strength);


                    } else {
                        System.out.println("Strength not found");
                        //send signal for out of scope to output
                        publishProgress("-1");
                        //return "-1";
                    }
                } else {
                    System.out.println("Current data is not okay");
                    publishProgress("Current data is not okay");
                    //send signal for satellite down to output
                    publishProgress("-1");
                    //return "-1";
                }

            } else {
                System.out.println("Data Date not okay");
                publishProgress("Data Date not okay");
                //send signal for satellite down to output
                publishProgress("-1");
                //return "-1";
            }


            //catch statements
        } catch (MalformedURLException e) {
            System.out.println("MalformedUrlException for dI allocation");
        } catch (IOException e) {
            System.out.println("IOException in dI alloc");

            //check inet conn/send satellite down to output
            return;
        }
        //return "ERROR";
    }
}

Что я сделал не так?

2 ответа

  1. При использовании IntentService создается отдельный поток для выполнения задачи в команде, после чего он завершается. Вся работа должна выполняться в методе onHandleIntent (Intent intent).
    В вашем случае используйте обычный сервис, а не IntentService.

  2. Просто для завершения темы; вот краткий ответ:

    Как сказал мне Алексей Шутов, я изменил сервис Intent на сервис.
    Я сделал это, следуя официальной документации:
    Документация

    Для всех заинтересованных в коде и / или проекте:
    GitHub цель