我有一個PictureHDFragment
中,我使用的方法Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
;我想的方法getDrawable
以下載圖像出來的片段的,並避免所有應用的阻擋啓動一個線程(其由Html.fromHtml
當遇到標記img
自動調用)。了java.lang.RuntimeException:無法實例化服務......沒有無參數的構造
然後我創建了一個外部類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)
所以,我明白,這個問題是傳遞給製造商的參數,那我也tryed到刪除,並獲得上下文直接在HttpImageGetter
類的方法getApplicationContext
但這扔NullPointerException異常。
我該如何解決這個問題?
感謝
提示:服務*** ***是一個上下文 –