0

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

发送短信.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
}

}

日志猫:

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)
4

2 回答 2

4

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

如果您这样做,您的程序将运行良好

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

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

于 2013-11-12T15:49:35.107 回答
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);
于 2013-06-13T05:59:26.343 回答