0

我正在尝试创建一个简单的应用程序,它只做一个简单的工作:我按下“启动服务”按钮,然后出现 Toast,并更改了 TextView(与停止服务相同)。但是当我尝试运行应用程序时,我的 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>

服务接口类:

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

    }

}

服务执行类:

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
4

2 回答 2

2

mServiceHandler 在您的 onStartCommand 中为空。尝试在该函数中初始化它。

如果您添加一个简单的打印:

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 中。

这里有原因:当调用 Service 类的 onCreate 和 onStartCommand 时

于 2013-06-16T17:59:50.197 回答
0

我认为您的处理程序在 onStartCommand 中可能为空。由于您使用的是处理程序线程,因此您应该等待该线程上的 Looper 准备就绪,然后使用该线程的 Looper 初始化处理程序。

于 2013-06-16T17:59:35.020 回答