2010-06-21 6 views
-1

depuis longtemps j'essaie de développer une application qui informerait l'utilisateur de tout message entrant via la parole, j'ai trois classes, TextSpeaker, Receiver et SpeakerService. Quand je commence l'application et cliquez sur le bouton Démarrer, je reçois une erreur d'exécution:ne peut pas trouver l'erreur avec l'application dans android

06-21 13:54:36.088: ERROR/AndroidRuntime(528): Uncaught handler: thread main exiting due to uncaught exception 
06-21 13:54:36.119: ERROR/AndroidRuntime(528): java.lang.RuntimeException: Unable to  start service [email protected] with Intent { cmp=com.example.TextSpeaker/.SpeakerService }: java.lang.NullPointerException 
06-21 13:54:36.119: ERROR/AndroidRuntime(528):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2882) 
.... 

06-21 13:54:36.119: ERROR/AndroidRuntime(528): Caused by: java.lang.NullPointerException 
06-21 13:54:36.119: ERROR/AndroidRuntime(528):  at com.example.TextSpeaker.SpeakerService.onStart(SpeakerService.java:33) 
06-21 13:54:36.119: ERROR/AndroidRuntime(528):  at android.app.Service.onStartCommand(Service.java:306) 
06-21 13:54:36.119: ERROR/AndroidRuntime(528):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2873) 
06-21 13:54:36.119: ERROR/AndroidRuntime(528):  ... 10 more 

Voici mes 3 classes: TEXTSPEAKER CLASSE:

package com.example.TextSpeaker; 

import java.util.Locale; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

// the following programme converts the text to speech 

public class TextSpeaker extends Activity implements OnInitListener { 
/** Called when the activity is first created. */ 
int MY_DATA_CHECK_CODE = 0; 
public TextToSpeech mtts; 
public Button button,stop_button; 
//public EditText edittext; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button = (Button)findViewById(R.id.button); 
    stop_button=(Button)findViewById(R.id.stop_button); 

    Intent myintent = new Intent(); 
    myintent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(myintent, MY_DATA_CHECK_CODE); 
    //edit text=(EditText)findViewById(R.id.edittext); 
} 

    public void buttonClickListener(View src){ 
     switch(src.getId()) 
     { 
     case(R.id.button): 
      Toast.makeText(getApplicationContext(), "The service has been started\n Every new message will now be read out", Toast.LENGTH_LONG).show(); 
      startService(new Intent(this,SpeakerService.class)); 
      break; 
     case(R.id.stop_button): 
      Toast.makeText(getApplicationContext(), "The service has been stopped\n ", Toast.LENGTH_LONG).show(); 
      stopService(new Intent(this,SpeakerService.class)); 
      break; 
     } 
    } 



    protected void onActivityResult(int requestcode,int resultcode,Intent data) 
    { 
     if(requestcode == MY_DATA_CHECK_CODE) 
     { 
      if(resultcode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 
      { 
       // success so create the TTS engine 
       mtts = new TextToSpeech(this,this); 
       mtts.setLanguage(Locale.ENGLISH); 

      } 
      else 
      { 
       //install the Engine 
       Intent install = new Intent(); 
       install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(install); 
      } 
     } 

    } 
    public void onDestroy(Bundle savedInstanceStatBundle) 
    { 
     mtts.shutdown(); 
    } 

    //public void onPause() 
    //{ 
    // super.onPause(); 
    // // if our app has no focus 
    // if(mtts!=null) 
    // mtts.stop(); 
    // } 
    @Override 
    public void onInit(int status) { 
     if(status==TextToSpeech.SUCCESS) 
      button.setEnabled(true); 

    } 


} 

RÉCEPTEUR CLASSE:

package com.example.TextSpeaker; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; // supports both gsm and cdma 
import android.util.Log; 
import android.widget.Toast; 



public class Receiver extends BroadcastReceiver{ 

//TextSpeaker tsp=new TextSpeaker(); 

public String str=""; 
@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    Log.d("Receiver","Message received successfully"); 

    SmsMessage[] msgs = null; 

    if(bundle!=null) 
    { 
     // retrive the sms received 

     Object[] pdus = (Object[])bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length]; 
     for(int i=0;i<msgs.length;i++) 
     { 
      msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]); 
      str+="Message From "+msgs[i].getOriginatingAddress()+"."; 
      str+="The message is "+msgs[i].getMessageBody().toString(); 
     } 
     Toast.makeText(context,str,Toast.LENGTH_SHORT).show(); 


    } 



} 

}

SERVICESPEAKER CLASSE:

package com.example.TextSpeaker; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.widget.Toast; 


public class SpeakerService extends Service { 

Receiver rv = new Receiver(); 
TextSpeaker tspker = new TextSpeaker(); 
//public TextToSpeech mtts; 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void onCreate(){ 
    //mtts =new TextToSpeech(getBaseContext(), null); 
    Log.d("SpeakerService","Service created successfully!"); 
    //mtts.speak(rv.str, TextToSpeech.QUEUE_FLUSH,null); 

} 
@Override 
public void onStart(Intent intent,int startid) 
{ 
    Log.d("SpeakerService","Service started successfully!"); 
    tspker.mtts.speak(rv.str, TextToSpeech.QUEUE_FLUSH,null); 
} 
@Override 
public void onDestroy(){ 
    if(tspker.mtts!=null) 
    { 
     tspker.mtts.stop(); 
     Toast.makeText(getApplicationContext(),"The service has been destroyed!", Toast.LENGTH_SHORT).show(); 
    } 

} 

}

Répondre

1

ressemble tspker.mtts est NULL.

ajouter une exploitation forestière dans SpeakerService.onStart, pour vérifier que, ou d'autres cas NULL:

public void onStart(Intent intent,int startid) 
{ 
    Log.d("SpeakerService","Service started successfully!"); 
    Log.d("SpeakerService","rv = " + rv.toString()); 
    Log.d("SpeakerService","tspker = " + tspker.toString()); 
    Log.d("SpeakerService","tspker.mtts = " + tspker.mtts.toString()); 
    tspker.mtts.speak(rv.str, TextToSpeech.QUEUE_FLUSH,null); 
} 
+0

J'ai essayé et obtenu: DEBUG/SpeakerService (561): Le service créé avec succès! DEBUG/SpeakerService (561): Le service a démarré avec succès! DEBUG/SpeakerService (561): Le service a démarré avec succès! debug/SpeakerService (561): vr = [email protected] debug/SpeakerService (561): tspker = [email protected] debug/AndroidRuntime (561): Arrêt VM suivi de la même erreur – pranay

+0

si tspker.mtts est null alors pouvez-vous s'il vous plaît suggérer, comment le corriger? – pranay

+0

Donc votre 'tspker.mtts' est définitivement NULL alors. Double-vérifier où vous l'initialisez –

Questions connexes