"无法启动服务意向和QUOT;当从Android中的活动启动错误的服务 [英] "Unable to start service Intent" error when starting service from an Activity in Android

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

问题描述

试图用一个CheckBox我MyActivity活动启动一个名为服务为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

我使用的教程<一href="http://developer.android.com/resources/tutorials/views/hello-formstuff.html">http://developer.android.com/resources/tutorials/views/hello-formstuff.html并补充提供的code到我的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中,我曾在&lt;服务>后声明&LT;应用程序>,而不是嵌套在它里面。

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天全站免登陆