我有一个我正在尝试启动的IntentService.当我这样做时,它会吐出这个:
java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173)
... (omitted for brevity)
Caused by: java.lang.NullPointerException
at android.app.IntentService.onStart(IntentService.java:110)
at android.app.IntentService.onStartCommand(IntentService.java:118)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2160)
... 10 more
Run Code Online (Sandbox Code Playgroud)
我已经搜索了这个,并查看了尽可能多的类似StackOverflow问题.然而,有一些微妙的差异,我无法解决.首先,异常中没有引用任何类.其次,通过更改上下文或双重检查来确定类似的问题,以确保它不为空.
我有代码来检查是不是这样:
public Context context;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
context = getApplicationContext();
if(context == null)
Log.d("PECAPP","Context is null");
setContentView(R.layout.news_layout);
...Omit button code...
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
Intent i = new Intent(context, NewsService.class); // also tried NewsActivity.this
if( i != null) // I know that …Run Code Online (Sandbox Code Playgroud)