2013-08-19 2 views
0

서비스에 대한 나의 첫 번째 시도. 내 서비스는 동적으로 가져 오는 파일 이름 문자열을 기반으로 서버에서 이미지 파일을 다운로드하기위한 것입니다.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); 
     } 
} 

답변

0

:

Intent intent = new Intent(context, DownloadPicture.class); 
intent.putExtra(DownloadPicture.FILENAME, filename); 
startService(intent); 
System.err.println("service started"); 

이 내 서비스입니다. 그래서 같은 IntentService에 대한 빈 인수가없는 디폴트 구성을하려고 :

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

No empty constructor when create a service는 희망이 도움을 참조하십시오.

+0

즉각적인 문제가 해결되었습니다. 이제 모든 새로운 문제를 해결해야합니다 ... 감사합니다! – Alex

+0

BTW, 게시하기 전에 빈 생성자를 만들려고했습니다. 내 실수는'super ("DownloadPicture")' – Alex

+0

ohh 대신에'super();'를 사용했다는 것입니다. 때때로 발생합니다. 지금 일하는 것이 좋습니다. –

0

매니페스트에 서비스를 추가하셨습니까?

<service android:name=".DownloadPicture" />

+0

예, 이미 매니페스트에 있습니다. 감사! – Alex

관련 문제