Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

java - intentservice no empty constructor, but there is a constructor

I have an IntentService within an Activity, and when I try to call the service, it throws this error, which I find strange because if I have the empty constructor declared.

Error:

06-17 15:48:34.603: E/AndroidRuntime(13363): FATAL EXCEPTION: main
06-17 15:48:34.603: E/AndroidRuntime(13363): java.lang.RuntimeException: Unable to instantiate service cl.prosys.rac.activity.HomeActivity$UploadService: java.lang.InstantiationException: can't instantiate class cl.prosys.rac.activity.HomeActivity$UploadService; no empty constructor
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2380)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.app.ActivityThread.access$1700(ActivityThread.java:134)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.os.Looper.loop(Looper.java:137)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.app.ActivityThread.main(ActivityThread.java:4867)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at java.lang.reflect.Method.invoke(Method.java:511)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at dalvik.system.NativeStart.main(Native Method)
06-17 15:48:34.603: E/AndroidRuntime(13363): Caused by: java.lang.InstantiationException: can't instantiate class cl.prosys.rac.activity.HomeActivity$UploadService; no empty constructor
06-17 15:48:34.603: E/AndroidRuntime(13363):    at java.lang.Class.newInstanceImpl(Native Method)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at java.lang.Class.newInstance(Class.java:1319)
06-17 15:48:34.603: E/AndroidRuntime(13363):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2377)
06-17 15:48:34.603: E/AndroidRuntime(13363):    ... 10 more

AndroidManifest.xml

<application>
    ...
    <service android:name=".activity.HomeActivity$UploadService"></service>
</application>

Activity:

package cl.prosys.rac.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.IntentService;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import cl.prosys.rac.R;
import cl.prosys.rac.broadcast.UploadBroadcastReceiver;
import cl.prosys.rac.config.AppConfig;
import cl.prosys.rac.config.DataKey;
import cl.prosys.rac.db.DataBase;
import cl.prosys.rac.db.DataBaseFactory;
import cl.prosys.rac.db.DataBasePrm;
import cl.prosys.rac.holder.HomeItemMenuHolder;
import cl.prosys.rac.net.Request;

public class HomeActivity extends AppActivity {

    ...

    public void onListItemClick(int position) {
        Intent intent;

        switch (position) {
        case 3: // Subir 
            intent = new Intent(this, UploadService.class);
            startService(intent);
            break;
        }
    }

    public class UploadService extends IntentService {

        public UploadService() {
            super("UploadService");
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            ...
        }

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public class UploadService extends IntentService {

Your Service is an inner class. If you want to keep it inside Activity, change it to static:

public static class UploadService extends IntentService {

You may want to read about different types of nested classes. First link from google: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...