java.lang.RuntimeException : Unable to start service java. lang.NullPointerException

标签 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)

最佳答案

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

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

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

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

关于java.lang.RuntimeException : Unable to start service java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079861/

相关文章:

java - 即使在构建路径中使用 ojdbc14.jar,Class.forName ("oracle.jdbc.driver.OracleDriver") 也会获取 ClassNotFoundException

java - 通过结合前瞻和后视,使用正则表达式在java中分割字符串

android - 更改 Android 设备管理设置

android - 启动 Intent.ACTION_SEND 总是返回 resultCode 0

android - sqlite 失败无法从警报服务中的游标读取游标窗口的第 0 行 col -1

Android 服务运行一段时间后停止运行请帮忙

java - 如何排除除@ComponentScan 下的一个类之外的所有@Component 类?

android - 我需要做什么才能为 Android (NDK) 构建(部分)WebRTC?它找不到 C++ header

linux - 获取以其他用户身份启动的后台进程的 PID

java - CPU 限制会导致 k8s 中的 OOMKilled 状态吗?