2016-11-08 115 views
-1

我试图将我的应用程序的一部分转换为服务,因为它需要在后台运行。现在,按钮应启动点击时停止该服务,:服务未启动 - 空对象参考

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 [email protected] 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.access$2200(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 xmlns:android="http://schemas.android.com/apk/res/android" 
    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"; 
    } 
} 

我做了什么错?

+1

您可以发布清单 –

+0

我加入清单的问题 –

+0

确定岗位服务以及 –

回答

0

只是为了完成主题;这里是对答案的总结:

就像Alex Shutov告诉我的,我将Intent Service改为服务。 我做到了这一点通过下面的官方文档: Documentation

对于所有感兴趣的代码和/或项目: GitHub AIM

0

您使用IntentService,它创建一个单独的线程来执行命令中的任务,然后它立即终止。你应该在onHandleIntent(Intent intent)方法中做所有的工作。 在你的情况下使用普通服务,而不是IntentService。

+0

在我的服务中有一个onCreate,它会执行一些小事...而主要工作是在onHandleIntent(Intent intent)中完成的 - 我没有看到我的错误... –

+0

没有停止IntentService的意思,一旦工作完成,它将自动停止 –

+0

该服务是一种永不终止的计算,可以生成发送到音频输出的音调。 所以我认为停止服务将是一个好主意,如果有人厌倦了噪音= P –