尝试调用虚方法' java.lang.String android.content.Intent.getStringExtra(java.lang.String)'在null对象引用上

时间:2017-08-15 11:46:13

标签: android android-intent

我正在使用服务但是当我在getExtraString()上关闭应用程序时它会崩溃

这是代码

 public int onStartCommand(Intent startingIntent,int flags,int startid)
    {
        msg = startingIntent.getStringExtra("MESSAGE");
        tts = new TextToSpeech(this,this);
        return START_STICKY;
    }

应用程序崩溃在msg = startingIntent.getStringExtra(" MESSAGE");

以下是在BroadcastReceiver

中启动服务的代码
 if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            String name = getContactName(context, number);
            Intent speechIntent = new Intent();
            speechIntent.setClass(context, TTSServices.class);
            speechIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |  Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            if (name == null) {
                speechIntent.putExtra("MESSAGE","Phone call from "+ number);
                Toast.makeText(context, number, Toast.LENGTH_SHORT).show();
            } else {
                speechIntent.putExtra("MESSAGE", "Phone call From "+name);
                Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
            }
            context.startService(speechIntent);
        }

0 个答案:

没有答案