java.lang.RuntimeException:无法启动服务java.lang.NullPointerException

时间:2013-06-13 05:34:47

标签: java android service

这是我的第一个使用服务的应用。获取java.lang.RuntimeException:无法启动服务java.lang.NullPointerException。我的目的是在设备处于挂起状态时发送短信。

SENDSMS.java

package com.qualcomm.sendsms;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class SENDSMS extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sendsms);
    String phone_num =  null;
    String sms = null;
    String sleep_time = null;
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        phone_num = extras.getString("Phone_Number");
        Log.e("????????????????SEND_SMS", "phno : "+phone_num);
        sms = extras.getString("SMS_Body");
        Log.e("????????????????SEND_SMS", "sms : "+sms);
        sleep_time = extras.getString("Sleep_Time");
        Log.e("????????????????Sleep_Time", "sleep_time : "+sleep_time);
        Intent myIntent = new Intent(this, sendservicesms.class);
        myIntent.putExtra("Phone_Number",phone_num);
        myIntent.putExtra("SMS_Body",sms);
        myIntent.putExtra("Sleep_Time",sleep_time);
        startService(myIntent);
    }
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.sendsm, menu);
    return true;
}

}

服务:sendservicesms.java

 package com.qualcomm.sendsms;

 import android.app.IntentService;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.telephony.SmsManager;
 import android.util.Log;
 import android.widget.Toast;

 public class sendservicesms extends IntentService  {
int mStartMode;       // indicates how to behave if the service is killed
IBinder mBinder;      // interface for clients that bind
boolean mAllowRebind; // indicates whether onRebind should be used

public sendservicesms() {
    super("sendservicesms");
  }
public void onCreate() {
    // The service is being created

}
@Override
protected void onHandleIntent(Intent intent) { 
    // The service is starting, due to a call to startService()
     if(intent!=null) {
    Bundle param = intent.getExtras();
    if (param != null) {
    String phone_no = (String)param.get("Phone_Number");
    String sms_body = (String)param.get("SMS_Body");
    String sleeptime = (String)param.get("Sleep_Time");
    Log.e("????????????????SEND_SMS", "phno : "+phone_no);
    Log.e("????????????????SEND_SMS", "sms : "+sms_body);
    Log.e("????????????????Sleep_Time", "sleep_time : "+sleeptime);
    Long time = Long.parseLong(sleeptime);
    Log.e("????????????????time long", "Long_time : "+time);
    try {
        if(sleeptime!=null && sleeptime.length() > 0){
            Thread.sleep(Long.parseLong(sleeptime));
        }
        Log.e("????????????????Sleep happened well", "sleep_time : "+sleeptime);
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phone_no, null, sms_body, null, null);
        Toast.makeText(getApplicationContext(), "SMS Sent!",
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "SMS faild, please try again later!",
                Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
    }
     }

}
@Override
public IBinder onBind(Intent intent) {
    // A client is binding to the service with bindService()
    return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
    // All clients have unbound with unbindService()
    return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
    // A client is binding to the service with bindService(),
    // after onUnbind() has already been called
}
@Override
public void onDestroy() {
    // The service is no longer used and is being destroyed
}

}

logcat的:

E/AndroidRuntime(10276): FATAL EXCEPTION: main
E/AndroidRuntime(10276): java.lang.RuntimeException: Unable to start service com.qualcomm.sendsms.sendservicesms@418dd170 with Intent { cmp=com.qualcomm.sendsms/.sendservicesms (has extras) }: java.lang.NullPointerException
E/AndroidRuntime(10276):        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2676)
E/AndroidRuntime(10276):        at android.app.ActivityThread.access$1900(ActivityThread.java:144)
E/AndroidRuntime(10276):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1334)
E/AndroidRuntime(10276):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(10276):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(10276):        at android.app.ActivityThread.main(ActivityThread.java:5074)
E/AndroidRuntime(10276):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10276):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(10276):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(10276):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(10276):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10276): Caused by: java.lang.NullPointerException
E/AndroidRuntime(10276):        at android.app.IntentService.onStart(IntentService.java:116)
E/AndroidRuntime(10276):        at android.app.IntentService.onStartCommand(IntentService.java:130)
E/AndroidRuntime(10276):        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2659)

2 个答案:

答案 0 :(得分:4)

你必须在onCreate()内的第一行调用super.onCreate(),如果你覆盖它,原因系统需要在你自己的oncreate()实现之前准备.....

如果您这样做,您的程序将正常运行

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Your rest of codes
}

当您从Activity中删除onCreate()时,系统会自行执行此操作。 :)

答案 1 :(得分:0)

每当有一个空指针异常时,它主要是由于xml ...某些元素可能在你的xml文件中不匹配。

发送短信你可以尝试以下代码:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", sms_text_string);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);