Reputation: 1772
I'm trying to create a simple app which just do a simple job: I press "Start Service" button and Toast appears with a TextView changed(same with Stop Service). But when I trying to run app, I get bunch of errors in my LogCat and my app stops working.
Here is activity_main.xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ServiceInterface" >
<TextView
android:id="@+id/tvResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Service Example"
android:textSize="20dp" />
<Button
android:id="@+id/bStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvHeader"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="Start Service" />
<Button
android:id="@+id/bStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/bStart"
android:layout_below="@+id/bStart"
android:text="Stop Service" />
</RelativeLayout>
My Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.serviceexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.serviceexample.ServiceInterface"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.serviceexample.ServiceExecution" >
</service>
</application>
</manifest>
ServiceInteface class:
package com.example.serviceexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ServiceInterface extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare buttons for Start and Stop
Button startService = (Button) findViewById(R.id.bStart);
Button stopService = (Button) findViewById(R.id.bStop);
// Initialize serviceStatus text field in display
TextView tv = (TextView) findViewById(R.id.tvResults);
tv.setText("Service not Running");
// Set listeners for buttons
startService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Start the Service
Intent startService = new Intent(ServiceInterface.this,
ServiceExecution.class);
startService(startService);
// Display Service running message
TextView start = (TextView) findViewById(R.id.tvResults);
start.setText("Service is running");
}
});
stopService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Start the Service
Intent stopService = new Intent(ServiceInterface.this,
ServiceExecution.class);
startService(stopService);
// Display Service running message
TextView stop = (TextView) findViewById(R.id.tvResults);
stop.setText("Service stopped");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
ServiceExecution class:
package com.example.serviceexample;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;
public class ServiceExecution extends Service {
// Declare variables for Looper and ServiceHandler
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
public class ServiceHandler extends Handler {
// Create a constructor for class.
// Run once on creation of handler object.
public ServiceHandler(Looper looper) {
// Override super class to use looper provided
super(looper);
}
// Handler receives message and carries out the work of the service
public void handleMessage(Message msg) {
// Wait before toasting message appears
// to give the Service Started message time to display
for (int i = 0; i <= 30; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Toast service message
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Service Message!!!",
Toast.LENGTH_LONG);
toast.show();
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
// Create a thread and service handler with a looper
public void OnCreate() {
super.onCreate();
// Create a Thread with a Looper
HandlerThread thread = new HandlerThread("ServiceStartArguments",
android.os.Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the threads looper
mServiceLooper = thread.getLooper();
// Create a service handler
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Get message from message pool using handler
Message msg = mServiceHandler.obtainMessage();
// Set start ID in message
msg.arg1 = startId;
// Send msg to start job
mServiceHandler.sendMessage(msg);
// Toast message Started Service
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Service Started!!!",
Toast.LENGTH_LONG);
toast.show();
// Start a Sticky
return START_STICKY;
}
public void OnDestroy() {
super.onDestroy();
// Toast Service Stopped
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "Service Stopped!!!",
Toast.LENGTH_LONG);
toast.show();
}
}
I just don't have a clue what is wrong with my code. Hope someone will see my fail. Appreciate any help from you guys :)
EDIT . Errors i get:
06-16 17:28:28.406: E/Trace(619): error opening trace file: No such file or directory (2)
06-16 17:28:31.496: E/AndroidRuntime(619): FATAL EXCEPTION: main
06-16 17:28:31.496: E/AndroidRuntime(619): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@4120a190 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:28:31.496: E/AndroidRuntime(619): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.os.Looper.loop(Looper.java:137)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:28:31.496: E/AndroidRuntime(619): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:28:31.496: E/AndroidRuntime(619): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:28:31.496: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:28:31.496: E/AndroidRuntime(619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:28:31.496: E/AndroidRuntime(619): at dalvik.system.NativeStart.main(Native Method)
06-16 17:28:31.496: E/AndroidRuntime(619): Caused by: java.lang.NullPointerException
06-16 17:28:31.496: E/AndroidRuntime(619): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:28:31.496: E/AndroidRuntime(619): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:28:31.496: E/AndroidRuntime(619): ... 10 more
06-16 17:28:43.035: E/Trace(639): error opening trace file: No such file or directory (2)
06-16 17:28:43.096: E/AndroidRuntime(639): FATAL EXCEPTION: main
06-16 17:28:43.096: E/AndroidRuntime(639): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@411ecd30 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:28:43.096: E/AndroidRuntime(639): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.os.Looper.loop(Looper.java:137)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:28:43.096: E/AndroidRuntime(639): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:28:43.096: E/AndroidRuntime(639): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:28:43.096: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:28:43.096: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:28:43.096: E/AndroidRuntime(639): at dalvik.system.NativeStart.main(Native Method)
06-16 17:28:43.096: E/AndroidRuntime(639): Caused by: java.lang.NullPointerException
06-16 17:28:43.096: E/AndroidRuntime(639): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:28:43.096: E/AndroidRuntime(639): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:28:43.096: E/AndroidRuntime(639): ... 10 more
06-16 17:30:04.816: E/Trace(678): error opening trace file: No such file or directory (2)
06-16 17:30:35.025: E/AndroidRuntime(678): FATAL EXCEPTION: main
06-16 17:30:35.025: E/AndroidRuntime(678): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@4120beb8 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:30:35.025: E/AndroidRuntime(678): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.os.Looper.loop(Looper.java:137)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:30:35.025: E/AndroidRuntime(678): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:30:35.025: E/AndroidRuntime(678): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:30:35.025: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:30:35.025: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:30:35.025: E/AndroidRuntime(678): at dalvik.system.NativeStart.main(Native Method)
06-16 17:30:35.025: E/AndroidRuntime(678): Caused by: java.lang.NullPointerException
06-16 17:30:35.025: E/AndroidRuntime(678): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:30:35.025: E/AndroidRuntime(678): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:30:35.025: E/AndroidRuntime(678): ... 10 more
06-16 17:30:42.326: E/Trace(693): error opening trace file: No such file or directory (2)
06-16 17:30:42.386: E/AndroidRuntime(693): FATAL EXCEPTION: main
06-16 17:30:42.386: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@411e5150 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:30:42.386: E/AndroidRuntime(693): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.os.Looper.loop(Looper.java:137)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:30:42.386: E/AndroidRuntime(693): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:30:42.386: E/AndroidRuntime(693): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:30:42.386: E/AndroidRuntime(693): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:30:42.386: E/AndroidRuntime(693): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:30:42.386: E/AndroidRuntime(693): at dalvik.system.NativeStart.main(Native Method)
06-16 17:30:42.386: E/AndroidRuntime(693): Caused by: java.lang.NullPointerException
06-16 17:30:42.386: E/AndroidRuntime(693): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:30:42.386: E/AndroidRuntime(693): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:30:42.386: E/AndroidRuntime(693): ... 10 more
06-16 17:33:04.446: E/Trace(753): error opening trace file: No such file or directory (2)
06-16 17:33:33.108: E/AndroidRuntime(753): FATAL EXCEPTION: main
06-16 17:33:33.108: E/AndroidRuntime(753): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@41205498 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:33:33.108: E/AndroidRuntime(753): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.os.Looper.loop(Looper.java:137)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:33:33.108: E/AndroidRuntime(753): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:33:33.108: E/AndroidRuntime(753): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:33:33.108: E/AndroidRuntime(753): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:33:33.108: E/AndroidRuntime(753): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:33:33.108: E/AndroidRuntime(753): at dalvik.system.NativeStart.main(Native Method)
06-16 17:33:33.108: E/AndroidRuntime(753): Caused by: java.lang.NullPointerException
06-16 17:33:33.108: E/AndroidRuntime(753): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:33:33.108: E/AndroidRuntime(753): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:33:33.108: E/AndroidRuntime(753): ... 10 more
06-16 17:33:41.275: E/Trace(768): error opening trace file: No such file or directory (2)
06-16 17:33:41.335: E/AndroidRuntime(768): FATAL EXCEPTION: main
06-16 17:33:41.335: E/AndroidRuntime(768): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@411e7bc8 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:33:41.335: E/AndroidRuntime(768): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.os.Looper.loop(Looper.java:137)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:33:41.335: E/AndroidRuntime(768): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:33:41.335: E/AndroidRuntime(768): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:33:41.335: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:33:41.335: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:33:41.335: E/AndroidRuntime(768): at dalvik.system.NativeStart.main(Native Method)
06-16 17:33:41.335: E/AndroidRuntime(768): Caused by: java.lang.NullPointerException
06-16 17:33:41.335: E/AndroidRuntime(768): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:33:41.335: E/AndroidRuntime(768): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:33:41.335: E/AndroidRuntime(768): ... 10 more
06-16 17:35:05.576: E/Trace(838): error opening trace file: No such file or directory (2)
06-16 17:35:17.266: E/AndroidRuntime(838): FATAL EXCEPTION: main
06-16 17:35:17.266: E/AndroidRuntime(838): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@41207c10 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:35:17.266: E/AndroidRuntime(838): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.os.Looper.loop(Looper.java:137)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:35:17.266: E/AndroidRuntime(838): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:35:17.266: E/AndroidRuntime(838): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:35:17.266: E/AndroidRuntime(838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:35:17.266: E/AndroidRuntime(838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:35:17.266: E/AndroidRuntime(838): at dalvik.system.NativeStart.main(Native Method)
06-16 17:35:17.266: E/AndroidRuntime(838): Caused by: java.lang.NullPointerException
06-16 17:35:17.266: E/AndroidRuntime(838): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:35:17.266: E/AndroidRuntime(838): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:35:17.266: E/AndroidRuntime(838): ... 10 more
06-16 17:35:24.806: E/Trace(852): error opening trace file: No such file or directory (2)
06-16 17:35:24.857: E/AndroidRuntime(852): FATAL EXCEPTION: main
06-16 17:35:24.857: E/AndroidRuntime(852): java.lang.RuntimeException: Unable to start service com.example.serviceexample.ServiceExecution@411ea5b8 with Intent { cmp=com.example.serviceexample/.ServiceExecution }: java.lang.NullPointerException
06-16 17:35:24.857: E/AndroidRuntime(852): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.app.ActivityThread.access$1900(ActivityThread.java:130)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.os.Looper.loop(Looper.java:137)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-16 17:35:24.857: E/AndroidRuntime(852): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 17:35:24.857: E/AndroidRuntime(852): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 17:35:24.857: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-16 17:35:24.857: E/AndroidRuntime(852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-16 17:35:24.857: E/AndroidRuntime(852): at dalvik.system.NativeStart.main(Native Method)
06-16 17:35:24.857: E/AndroidRuntime(852): Caused by: java.lang.NullPointerException
06-16 17:35:24.857: E/AndroidRuntime(852): at com.example.serviceexample.ServiceExecution.onStartCommand(ServiceExecution.java:78)
06-16 17:35:24.857: E/AndroidRuntime(852): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
06-16 17:35:24.857: E/AndroidRuntime(852): ... 10 more
Upvotes: 0
Views: 602
Reputation: 1067
mServiceHandler is null in your onStartCommand. Try to initialize it in that function.
If you add a simple print:
public void OnCreate() {
System.out.println("In on create");
and
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("In onStartCommand()");
You can easily see that your program only enters in the second function. Because of that, the handler results still uninitialized. You can completely get rid of the onCreate method and merge its logic in the onStartCommand.
Here you have the reason: when onCreate and onStartCommand on Service class are invoked
Upvotes: 2
Reputation: 286
I think Your Handler might be null in onStartCommand. Since you are using an handler thread, you should wait for the looper to be ready on that thread, and then Initialize the handler with that thread's looper.
Upvotes: 0