Android undefined intent constructor錯誤?

本文選自StackOverflow(簡稱:SOF)精選問答彙總系列文章之一,本系列文章將爲讀者分享國外最優質的精彩問與答,供讀者學習和了解國外最新技術。在Android中啓動Service時出現「undefined intent constructor」的錯誤,怎麼辦?html

問:Ramjava

我在Activity中嘗試啓動Service,但出現「undefined intent constructor」的報錯信息。android

MyService.java代碼以下:app

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的代碼以下:ide

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 pvsthis


(最佳答案)

你不該該使用Service.this,而應該按以下方法改變class:spa

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
相關文章
相關標籤/搜索