【问题标题】:Fatal signal 11 when trying to use AsyncTask尝试使用 AsyncTask 时出现致命信号 11
【发布时间】:2014-12-18 18:56:25
【问题描述】:

我整天都在努力解决这个问题,我正在尝试调用以下方法:

new DownloadandSaveAsync(Activity_picture.this).execute(image_url);

单击 ImageView 时会调用此方法。

但是一旦我的 AsyncTask 开始加载它就会关闭,没有崩溃报告,只是以下内容:

Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 6744 (AsyncTask #4)

这是我调用它的活动:

public class Activity_picture extends ActionBarActivity {

public static final int ALBUM_RESULT = 300;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_picture);

    final String image_url = getIntent().getStringExtra("image_url");
    String image_title = getIntent().getStringExtra("image_title");

    ImageView picture = (ImageView) findViewById(R.id.picture);
    TextView pictureTitle = (TextView) findViewById(R.id.pictureTitle);

    Picasso.with(getApplicationContext()).load(image_url).into(picture);

    pictureTitle.setText(image_title);

    picture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new DownloadandSaveAsync(Activity_picture.this)
                    .execute(image_url);

            finish();

        }
    });

}
}

然后这是我的 AsyncTask 代码:

public class DownloadandSaveAsync extends AsyncTask<String, String, String> {

private ProgressDialog pDialog;
Bitmap profPictBitmap;
Context context;

URL image_value;

public DownloadandSaveAsync(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub

    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

}

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub

    try {

        image_value = new URL(params[0]);

        System.out.println(image_value);

        profPictBitmap = BitmapFactory.decodeStream(image_value
                .openConnection().getInputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub

    File f = new File(context.getCacheDir(), "profileImage");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Convert bitmap to byte array
    Bitmap bitmap = profPictBitmap;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
    byte[] bitmapdata = bos.toByteArray();

    // write the bytes in file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fos.write(bitmapdata);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Intent newIntent = new Intent(context, FeatherActivity.class);
    newIntent.setData(Uri.parse(f.toString()));
    newIntent.putExtra(Constants.EXTRA_IN_API_KEY_SECRET,
            "SECRET KEY");
    ((Activity) context).startActivityForResult(newIntent, 1);

    if (pDialog != null) {
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

}

}

我 100% 确定正在执行的 url 不错,因为我已经完成了 System.out.println(image_value);,它返回正确的 URL,就像在 AsyncTask 类中一样。

我在我的应用程序的其他地方调用相同的 AsyncTask 代码来下载图像,它在传递其他 URL 时可以 100% 正常工作,但由于某些我无法理解的奇怪原因,它会因该错误而强制关闭。

如果有人能在这里帮助我,将不胜感激!提前致谢。

【问题讨论】:

  • 为什么要在 onClick 中调用 finish()?
  • 因为我想在调用异步任务后完成当前活动,所以我还尝试在不调用 finish(); 的情况下运行应用程序;海峡之后并没有做任何事情:(
  • 您能否尝试将DownloadandSaveAsync 更改为扩展AsyncTask&lt;String, String, Bitmap&gt;,然后在doInBackground 中返回您已解码的位图,而不是使用实例变量来存储位图。我这样说是因为我不能相信onPostExecute 会被同步调用。
  • 启动一个 AsyncTask 然后立即调用完成如果没有错的话是很臭的 - 什么上下文拥有 AsyncTask?您正在传递一个上下文,这意味着您的活动将被保留并泄露。如果您需要继续执行任务并且完成原始活动,则它应该由服务持有。到下载完成时,您的 Context 不再有效,这将导致所有类型的问题。当您尝试在不再有效的 Context 中操作视图和对话框时,您还将在 onPostExecute 中遇到问题。
  • @GabeSechan:这是完全错误的,特别是因为 AsyncTask 正在使用 Activity 来显示 ProgressDialog。

标签: android android-asynctask


【解决方案1】:

在我使用 Asynctask 的情况下,我遇到了同样的错误

    <uses-permission android:name="android.permission.INTERNET" />

我只是在清单文件中添加互联网权限。之后它对我有用

【讨论】:

    猜你喜欢
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2016-04-22
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多