서비스에 대한 나의 첫 번째 시도. 내 서비스는 동적으로 가져 오는 파일 이름 문자열을 기반으로 서버에서 이미지 파일을 다운로드하기위한 것입니다.Android 서비스가 인스턴스화되지 않음
다음 오류가 발생합니다. 누구든지 내가 뭘 잘못하고 있는지 알아? 고맙습니다! 여기
08-19 16:40:18.102: E/AndroidRuntime(27702): java.lang.RuntimeException: Unable to instantiate service database.DownloadPicture: java.lang.InstantiationException: can't instantiate class database.DownloadPicture; no empty constructor
내가 서비스를 시작하고 방법은 다음과 같습니다 no empty constructor
: 당신은 신중하게 말한다 오류를 읽으면
public class DownloadPicture extends IntentService {
private int result = Activity.RESULT_CANCELED;
public static final String FILENAME = "filename";
public static final String FILEPATH = "filepath";
public static final String RESULT = "result";
public static final String NOTIFICATION = "com.mysite.myapp";
public DownloadPicture(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
String urlPath = this.getResources().getString(R.string.imagesURL);
String fileName = intent.getStringExtra(FILENAME);
File output = new File(Environment.getExternalStorageDirectory(), fileName);
if (output.exists()) {output.delete();}
InputStream stream = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
fos = new FileOutputStream(output.getPath());
int next = -1;
while ((next = reader.read()) != -1) {
fos.write(next);
}
// Successful finished
result = Activity.RESULT_OK;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publishResults(output.getAbsolutePath(), result);
}
private void publishResults(String outputPath, int result) {
Intent intent = new Intent(NOTIFICATION);
intent.putExtra(FILEPATH, outputPath);
intent.putExtra(RESULT, result);
sendBroadcast(intent);
}
}
즉각적인 문제가 해결되었습니다. 이제 모든 새로운 문제를 해결해야합니다 ... 감사합니다! – Alex
BTW, 게시하기 전에 빈 생성자를 만들려고했습니다. 내 실수는'super ("DownloadPicture")' – Alex
ohh 대신에'super();'를 사용했다는 것입니다. 때때로 발생합니다. 지금 일하는 것이 좋습니다. –