我正在尝试创建一个简单的应用程序,该应用程序仅完成一项简单的工作:我按“启动服务”按钮,并且Toast出现了TextView更改(与Stop Service相同)。但是,当我尝试运行应用程序时,我的LogCat出现了很多错误,并且我的应用程序停止运行。
这是activity_main.xml代码:
<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>
我的清单代码:
<?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类:
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类:
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();
}
}
我只是不知道我的代码有什么问题。希望有人能看到我的失败。感谢你们的帮助:)
编辑。我得到的错误:
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
最佳答案
在您的onStartCommand中,mServiceHandler为null。尝试在该函数中对其进行初始化。
如果添加简单的打印:
public void OnCreate() {
System.out.println("In on create");
和
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("In onStartCommand()");
您可以轻松地看到您的程序仅进入第二个功能。因此,处理程序结果仍未初始化。您可以完全摆脱onCreate方法,并在onStartCommand中合并其逻辑。
这是您的原因:when onCreate and onStartCommand on Service class are invoked