"无法启动服务意图"从Android中的“活动"启动服务时出现错误 [英] "Unable to start service Intent" error when starting service from an Activity in Android

查看:189
本文介绍了"无法启动服务意图"从Android中的“活动"启动服务时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试在MyActivity活动中使用CheckBox来启动名为​​"MyService"的服务时,我在DDMS中看到以下错误:

I see the following error in DDMS when trying to use a CheckBox on my MyActivity" activity to start a service called "MyService":

W/ActivityManager(   73): Unable to start service Intent { cmp=com.example.android.myprogram/.MyService }: not found

我使用了 http://developer.android.com/resources/tutorials/views/hello-formstuff.html ,并将提供的代码添加到我的onCreate()方法的末尾.我在MyActivity.java和MyService.java中分别指定了这些类.

I used the tutorial http://developer.android.com/resources/tutorials/views/hello-formstuff.html and added the provided code to the end of my onCreate() method. I have the classes specified separately in MyActivity.java and MyService.java.

package com.example.android.myprogram;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;


public class MyActivity extends Activity {
    private static final String TAG = "MyActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
        checkbox.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks, depending on whether it's now checked
                if (((CheckBox) v).isChecked()) {
                    // TODO: Add code to START the service
                    Log.d(TAG, "startService from checkbox");     
                    startService(new Intent(MyActivity.this, MyService.class));
                } else {
                    // TODO: Add code to STOP the service
                    Log.d(TAG, "stopService from checkbox");     
                    stopService(new Intent(MyActivity.this, MyService.class));
                }
            }
        });
    }
}

我的清单文件确实包含以下内容,其中我还尝试了完整的名称空间,短名称,在每个其他搜索中使用意图过滤器等.我并不是在说什么是正确的.我只是把它停在了停靠点.

My manifest file does have the following in which I've also tried the full namespace, short name, using an intent-filter per another search, etc. I'm not saying what is there is correct. I just left it at a stopping point.

<service android:name=".MyService">
   <intent-filter><action android:name="com.example.android.myprogram.MyService"></action>
   </intent-filter>
</service>

最后,我决定细分为最低限度的服务:

And lastly, my service which I've decided to break down to it's bare minimum:

package com.example.android.myprogram;


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

public class MyService extends Service {
    private static final String TAG = "MyService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        //code to execute when the service is first created
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        //code to execute when the service is shutting down
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Log.d(TAG, "onStart");
        //code to execute when the service is starting up
    }
}

我对Java/Android编程和一般编程(但学习)非常陌生,因此我确定这是用户错误,可能是其他所有人的常识.任何建议都会很棒.

I'm very, very, very new to Java/Android programming and programming in general (but learning) so I'm sure this is user error and probably common sense to everyone else. Any suggestions would be great.

推荐答案

我一直四处搜寻,而且据我所知,我在犯一个明显的菜鸟错误.在AndroidManifest.xml中,我有<<之后的service>声明application>而不是嵌套在其中.

I kept digging around and, as I figured, I was making an obvious rookie error. In AndroidManifest.xml, I had the < service> declaration after < application> instead of nested inside it.

这篇关于&quot;无法启动服务意图&quot;从Android中的“活动"启动服务时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆