本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术。在Android中启动Service时出现“undefined
intent constructor
”的错误,怎么办?

问:Ram

我在Activity中尝试启动Service,但出现“undefined intent constructor”的报错信息。

MyService.java代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public
class MyService extends Service {
 
@Override
public
IBinder onBind(Intent intent) {
    return

null
;
}
 
public
static boolean isInstanceCreated() {
      return

instance !=
null;
   }
 
@Override
public
void onCreate() {
    Toast.makeText(this,
"My
Service Created"
,
Toast.LENGTH_LONG).show();
    Log.d(TAG,
"onCreate");
 
     instance
=
this;
}
 
@Override
public
void onDestroy() {
    Toast.makeText(this,
"My
Service Stopped"
,
Toast.LENGTH_LONG).show();
    Log.d(TAG,
"onDestroy");
    instance
=
null;
 
}
 
@Override
public
void onStart(Intent intent, int startid) {
            Toast.makeText(getBaseContext(),
"Service
started"
,Toast.LENGTH_SHORT).show();
    }
 
 
}

启动SampleService.java的代码如下:

1
2
3
4
5
6
7
8
9
  public
class SampleService extends Activity{
@Override
protected
void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_activity);
    Intent
myintent =
new

Intent(SampleService.
this,MyService.this);//Error
show here..
    startService(myintent);
      }
 }

在manifest file中设定service的初值如下:

1
<service
android:enabled=
"true"

android:name=
"com.MyApp.MyService"

/>

请大家帮我解决这个错误。

答:kalyan pvs

(最佳答案)

你不应该使用Service.this,而应该按如下方法改变class:

1
Intent
myintent =
new

Intent(SampleService.
this,MyService.Class);

答:Raghunandan

做如下调整:

1
Intent
myintent =
new

Intent(SampleService.
this,MyService.this);

变为:

1
2
Intent
myintent =
new

Intent(SampleService.
this,MyService.Class);
 //
first param is a context second param is a class in your case a MyServiceClass

你没有设置类似于Intent(SampleService, MyService)的构造函数,在intent constructor参数设定上出现错误。

1
2
3
4
5
6
7
8
9
10
public
Intent (Context packageContext, Class<?> cls)
 
Added
in

API level 1
Create
an intent
for

a specific component. All other fields (action, data, type, class) are
null,
though they can be modified later
with

explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class
for

you; see setComponent(ComponentName)
for

more information on the repercussions of
this.
 
Parameters
packageContext 
A Context of the application package implementing
this

class.
cls
 
The
component class that is to be used
for

the intent.

原文链接:http://stackoverflow.com/questions/20068783/errorundefined-intent-constructor-when-start-service-in-android

文章选自StackOverFlow社区,鉴于其内容对于开发者有所帮助,现将文章翻译于此,供大家参考及学习。9Tech将每日持续更新,读者可点击StackOverflow(简称:SOF)精选问答汇总,查看全部译文内容。同时,我们也招募志同道合的技术朋友共同翻译,造福大家!报名请发邮件至zhangqi_wj@cyou-inc.com。

来自:9Tech

Android undefined intent constructor错误?的更多相关文章

  1. android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误

    第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...

  2. android之intent显式,显式学习

    intent,意图 当从一个Activity到另一个Activity时调用,这里重点学习显式,隐式的使用 使用语句上的区别: 隐式意图:                 显式意图: setAction ...

  3. 【转】【Java/Android】Intent的简介以及属性的详解

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  4. (转载)android开发常见编程错误总结

    首页 › 安卓开发 › android开发 android开发常见编程错误总结 泡在网上的日子 / 文 发表于2013-09-07 13:07  第771次阅读 android,异常 0 编辑推荐:稀 ...

  5. Android 笔记 Intent and Bundle day7

    学习了Intent与Bundle的使用,进行应用中的交互 package com.example.intent; import android.app.Activity; import android ...

  6. Android 通过 Intent 传递类对象或list对象

    (转:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html) Android中Intent传递类对象提供了两种方式一种是 通过实现 ...

  7. Android中Intent传递对象的两种方法(Serializable,Parcelable)

    今天要给大家讲一下Android中 Intent中如何传递对象,就我目前所知道的有两种方法,一种是Bundle.putSerializable(Key,Object);另一种是 Bundle.putP ...

  8. Android中Intent传值与Bundle传值的区别详解

    Android中Intent传值与Bundle传值的区别详解 举个例子我现在要从A界面跳转到B界面或者C界面   这样的话 我就需要写2个Intent如果你还要涉及的传值的话 你的Intent就要写两 ...

  9. Xamarin Android提示内存溢出错误

    Xamarin Android提示内存溢出错误 错误信息:java.lang.OutOfMemoryError, Consider increasing the value of $(JavaMaxi ...

随机推荐

  1. LR 两种html与url录制

    一直在使用LR,对于Html_based script和Url-based script 两种录制方式之间,要如何选择,仍是一知半解.最近测试时遇到同样的业务功能,两种录制方式的脚本,单次执行时间差别 ...

  2. C# 父子页面传值

    业务需求是:父页面点击“选择任务”按钮进入任务列表页.(项目进度周报) 父页面如下: 任务列表页: 选择某一个任务,点击“确定”后返回父页面所需数据. 父页面“选择任务” 按钮触发事件. /// &l ...

  3. javascript中的 return false和return true

    关于javascript中的 return false和return true,return 是javascript里函数返回值的关键字,一个函数内处理的结果可以使用return 返回,这样在调用函数 ...

  4. findBugs安装

    点击“Help->InstallNew Software”,如下图所示: 2 接着如下图所示: 3 Name”输入“findBugs”,“Location”输入“http://findbugs. ...

  5. Azure系列2.1.7 —— BlobRequestOptions

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  6. [转帖]How To Be Successful

    How To Be Successful http://blog.samaltman.com/how-to-be-successful 总结一下文章的重点: 1. Compound yourself2 ...

  7. 转《在浏览器中使用tensorflow.js进行人脸识别的JavaScript API》

    作者 | Vincent Mühle 编译 | 姗姗 出品 | 人工智能头条(公众号ID:AI_Thinker) [导读]随着深度学习方法的应用,浏览器调用人脸识别技术已经得到了更广泛的应用与提升.在 ...

  8. Python魔法方法(magic method)细解几个常用魔法方法(上)

    这里只分析几个可能会常用到的魔法方法,像__new__这种不常用的,用来做元类初始化的或者是__init__这种初始化使用的 每个人都会用的就不介绍了. 其实每个魔法方法都是在对内建方法的重写,和做像 ...

  9. python学习笔记(5)-基本数据类型-字符串类型及操作

    一.字符串 字符串由一对单引号或者双引号表示,如”abc“,‘中国’,字符串是字符的有序序列,可以对其中的字符进行索引.字符串也可以用三单引号或三双引号表示,可以表示多行字符串,一对单引号或双引号仅表 ...

  10. python学习笔记(6)--条件分支语句

    if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...