java.lang.RuntimeException:无法实例化服务...没有零参数构造函数

时间:2017-09-20 09:42:53

标签: android runtimeexception

我有一个PictureHDFragment我使用方法Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler);我希望方法getDrawable(当遇到标记Html.fromHtml时由img自动调用)来启动一个线程,以便从Fragment中下载图像并避免阻塞所有申请。

然后我创建了一个实现HttpImageGetter并扩展ImageGetter的外部类IntentService

public class HttpImageGetter extends IntentService implements Html.ImageGetter {

private Bitmap bitmap = null;
private Context context;

public HttpImageGetter(Context context) {
    super("get HTML Image");
    this.context = context;
}

@Override
public Drawable getDrawable(String source) {
    //mvc.controller.getDrawable(getContext(), source);

    //Context context = getApplicationContext();
    startService(context, source);
    BitmapDrawable bitmapDrawable;

    if (bitmap == null)
        bitmapDrawable = null;
    else {
        //bitmapDrawable = bitmapDrawableIterator.next();
        bitmapDrawable = new BitmapDrawable(bitmap);
        bitmapDrawable.setBounds(0, 0, bitmapDrawable.getIntrinsicWidth(), bitmapDrawable.getIntrinsicHeight());
    }
    //BitmapDrawable bitmapDrawableExample = new BitmapDrawable(mvc.model.getHDImage());
    //bitmapDrawableExample.setBounds(0, 0, bitmapDrawableExample.getIntrinsicWidth(), bitmapDrawableExample.getIntrinsicHeight());

    return bitmapDrawable/*bitmapDrawableExample*/;
}

private final static String ACTION = "getImage";

private void startService(Context context, String source){
    Intent intent = new Intent(context/*getApplicationContext()*/, HttpImageGetter.class);
    intent.setAction(ACTION);
    intent.putExtra("source", source);
    context.startService(intent);
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    String source = intent.getStringExtra("source");
    //Bitmap dr = null;

    try {
        URL url = new URL(source);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        //dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但这不起作用并启动RuntimeException,特别是:

java.lang.RuntimeException: Unable to instantiate service com.android.mattia.flickclient.Controller.HttpImageGetter: java.lang.InstantiationException: java.lang.Class<com.android.mattia.flickclient.Controller.HttpImageGetter> has no zero argument constructor
                                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3446)
                                                                                    at android.app.ActivityThread.-wrap6(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                 Caused by: java.lang.InstantiationException: java.lang.Class<com.android.mattia.flickclient.Controller.HttpImageGetter> has no zero argument constructor
                                                                                    at java.lang.Class.newInstance(Native Method)
                                                                                    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3443)
                                                                                    at android.app.ActivityThread.-wrap6(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

所以我理解问题是传递给构建器的参数,然后我尝试删除它并通过方法HttpImageGetter直接在getApplicationContext类中获取上下文,但这会抛出NullPointerException。

如何解决此问题?

谢谢

1 个答案:

答案 0 :(得分:0)

尝试添加此代替现有构造函数:public HttpImageGetter() {super("get HTML Image");} 如果您需要服务中的上下文,只需将服务用作上下文。