【问题标题】:Android Service not InstantiatingAndroid服务未实例化
【发布时间】:2013-08-19 22:51:58
【问题描述】:

这是我第一次尝试使用服务。我的服务旨在根据动态获取的文件名字符串从服务器下载图像文件。

我收到以下错误。有谁看到我做错了什么?谢谢!

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

这是我启动服务的方式:

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

这是我的服务:

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);
      }
}

【问题讨论】:

    标签: android service


    【解决方案1】:

    如果您仔细阅读错误信息:no empty constructor。所以尝试为IntentService 设置一个空的默认无参数构造函数,例如:

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

    请参阅No empty constructor when create a service 希望对您有所帮助。

    【讨论】:

    • 看起来解决了眼前的问题。现在我只需要处理所有新问题...谢谢!
    • 顺便说一句,在发布此内容之前,我确实尝试过创建一个空的构造函数。我的错误是我使用了super(); 而不是super("DownloadPicture")
    【解决方案2】:

    您是否已将服务添加到清单中?

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

    【讨论】:

      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 2011-06-29
      • 2020-01-05
      • 2017-05-04
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多