intent服务导致App崩溃
intent service causes the App to crash
我用过Android服务,我知道它是如何工作的,但我不知道它和IntentService有什么区别。所以我创建了 MyIntentService class 如下所示,但是在 运行
应用程序崩溃并生成下面发布的 logCat 错误的时间。
请告诉我为什么会收到这些错误以及如何解决
MainActivity
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private Button mbtnSend = null;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mbtnSend = (Button) findViewById(R.id.btn_send);
this.mbtnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyIntentService.class);
intent.putExtra("intent_key", ++i);
startService(intent);
}
});
}
}
MyIntentService:
public class MyIntentService extends IntentService {
private final String TAG = this.getClass().getSimpleName();
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public MyIntentService(String name) {
super(name);
setIntentRedelivery(true);
}
@Override
public void onCreate() {
super.onCreate();
Log.w(TAG, SubTag.msg("onCreate"));
}
@Override
protected void onHandleIntent(Intent intent) {
Log.w(TAG, SubTag.msg("onHandleIntent"));
int intent_value = intent.getIntExtra("intent_key", -1);
Log.i(TAG, SubTag.bullet("", "intent_value: " + intent_value));
SystemClock.sleep(3000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, SubTag.msg("onStartCommand"));
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, SubTag.msg("onDestroy"));
}
}
logcat:
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: FATAL EXCEPTION: main
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: Process: com.example.com.intentservice_00, PID: 18094
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service com.example.com.intentservice_00.MyIntentService: java.lang.InstantiationException: java.lang.Class<com.example.com.intentservice_00.MyIntentService> has no zero argument constructor
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3778)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:223)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1885)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7231)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: Caused by: java.lang.InstantiationException: java.lang.Class<com.example.com.intentservice_00.MyIntentService> has no zero argument constructor
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.Class.newInstance(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3775)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:223)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1885)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7231)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
试试这个.. 创建默认构造函数
public class MyIntentService extends IntentService{
public MyIntentService(){
super(null);// That was the lacking constructor
}
public MyIntentService(String name) {
super(name);
}
//...
}
希望对您有所帮助
我用过Android服务,我知道它是如何工作的,但我不知道它和IntentService有什么区别。所以我创建了 MyIntentService class 如下所示,但是在 运行 应用程序崩溃并生成下面发布的 logCat 错误的时间。
请告诉我为什么会收到这些错误以及如何解决
MainActivity
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().getSimpleName();
private Button mbtnSend = null;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mbtnSend = (Button) findViewById(R.id.btn_send);
this.mbtnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MyIntentService.class);
intent.putExtra("intent_key", ++i);
startService(intent);
}
});
}
}
MyIntentService:
public class MyIntentService extends IntentService {
private final String TAG = this.getClass().getSimpleName();
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public MyIntentService(String name) {
super(name);
setIntentRedelivery(true);
}
@Override
public void onCreate() {
super.onCreate();
Log.w(TAG, SubTag.msg("onCreate"));
}
@Override
protected void onHandleIntent(Intent intent) {
Log.w(TAG, SubTag.msg("onHandleIntent"));
int intent_value = intent.getIntExtra("intent_key", -1);
Log.i(TAG, SubTag.bullet("", "intent_value: " + intent_value));
SystemClock.sleep(3000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, SubTag.msg("onStartCommand"));
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, SubTag.msg("onDestroy"));
}
}
logcat:
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: FATAL EXCEPTION: main
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: Process: com.example.com.intentservice_00, PID: 18094
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service com.example.com.intentservice_00.MyIntentService: java.lang.InstantiationException: java.lang.Class<com.example.com.intentservice_00.MyIntentService> has no zero argument constructor
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3778)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:223)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1885)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7231)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: Caused by: java.lang.InstantiationException: java.lang.Class<com.example.com.intentservice_00.MyIntentService> has no zero argument constructor
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.Class.newInstance(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.handleCreateService(ActivityThread.java:3775)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.access00(ActivityThread.java:223)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1885)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:158)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7231)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
07-18 11:39:04.081 18094-18094/com.example.com.intentservice_00 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
试试这个.. 创建默认构造函数
public class MyIntentService extends IntentService{
public MyIntentService(){
super(null);// That was the lacking constructor
}
public MyIntentService(String name) {
super(name);
}
//...
}
希望对您有所帮助