2013-06-13 63 views
0

这是我第一个使用服务的应用程序。获取java.lang.RuntimeException:无法启动服务java.lang.NullPointerException。我的意图是当设备处于挂起状态时发送短信。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 [email protected] 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) 

回答

0

每当有一个空指针异常是主要是由于XML ...有些元素可能无法在XML文件中匹配。

发送短信U可以试试下面的代码:

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

我删除了下面的代码,它工作正常。 public void onCreate(){ //服务正在创建中' } –

4

您必须调用super.onCreate()里面的onCreate()的第一行,如果你重写它,因为系统需要自己的OnCreate前准备()实现.....

如果你这样做,你的程序将运行罚款

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

瓦你从你的Activity中删除onCreate(),系统会为你自己做这件事。 :)

相关问题