【问题标题】:Android Service Not Starting From ActivityAndroid 服务未从 Activity 启动
【发布时间】:2013-07-15 14:40:03
【问题描述】:

我是新来的,我有一个关于 android 服务的问题。出于某种原因,我的服务没有开始。并且没有在 logcat 中给出任何错误。我用过

startService(new Intent(getApplicationContext(),MyResultsService.class));

在主活动中调用服务(MyResultsService.class)。

另外,这是 MyResultsService.class:

package com.example.myapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyResultsService extends Service{
    public UpdateMyResults updater;
    public boolean stopThread = false;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d(getPackageName(), "Created MyResultsService");
    }

    @Override
    public synchronized int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Log.d(getPackageName(), "Starting...");
        if (!updater.isAlive()){
            updater = new UpdateMyResults();
            Log.d(getPackageName(), "New updater thread...");
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public synchronized void onDestroy() {
        // TODO Auto-generated method stub
        stopThread = true;
        Log.d(getPackageName(), "Destroying MyResultsService");
        try {
            updater.join();
            Log.d(getPackageName(), "Closed updater thread");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onDestroy();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    private class UpdateMyResults extends Thread{

        static final long DELAY = 30000;
        @Override
        public void run() {
            while (!stopThread){
                try {
                    //Do stuff and pause
                    Log.d(getName(), "Running");
                    Thread.sleep(DELAY);
                } catch (InterruptedException e) {
                    // Interrupt
                    e.printStackTrace();
                }
            }//end while
        }//end run
    }//end UpdateMyResults
}

我的 xml 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />

    <application
        android:label="MyApp"
        android:icon="@drawable/ic_launcher"
        android:theme="@android:style/Theme.Holo.Light">

        <activity
            android:name=".MainActivity"
            android:label="My App">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>      
        </activity>

        <service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" />

    </application>

</manifest>

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: android android-intent android-activity android-service android-xml


    【解决方案1】:

    &lt;service android:enabled="true" android:name="com.example.myapp.MYRESULTSSERVICE" /&gt; 中的名称必须与您的班级完全匹配。尽量不要全部大写。

    【讨论】:

    • 07-15 15:27:07.278: E/AndroidRuntime(8979): java.lang.RuntimeException: Unable to start service com.example.myapp.MyResultsService@40ecbab0 with Intent { cmp=com. example.myapp/.MyResultsService }: java.lang.NullPointerException 我现在得到这个错误
    • 您能否将完整的堆栈跟踪粘贴到 pastebin.com 或类似的地方并发布链接。
    • 第 23 行,您正在调用尚未初始化的变量 (updater) 上的方法,导致 NPE。
    • 哇,我永远不会发现。感谢您的帮助,我真的很感激!
    【解决方案2】:

    将清单中的服务标签更改为以下内容。

    <service android:enabled="true" android:name="com.example.myapp.MyResultsService" />
    

    【讨论】:

      【解决方案3】:

      您的清单包含错误的服务名称。它是 MYRESULTSSERVICE,但您的服务类名称是 MyResultsService。尝试从MYRESULTSSERVICE 更改为MyResultsService

      【讨论】:

        【解决方案4】:

        尝试在 Manifest 文件中使用以下内容:

        <service android:enabled="true" android:name="com.example.myapp.MyResultsService"/> 
        

        【讨论】:

          【解决方案5】:
          Line 23, you are calling a method on a variable (updater) that has not been initialized yet, causing a NPE. – Alex Fu Jul 15 '13 at 16:03
          

          你还没有开始线程。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-23
            • 2011-01-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多