Android 程序在 doInBackground 停止,没有进入 onPostExecute

Android program stops at doInBackground and doesn't come to onPostExecute

我的程序在 doInBackground 后崩溃,没有进入 onPostExecute。

我的activity代码相关部分是这样的:

public static class News {
    private String title;
    private String content;
    private Bitmap image;

    public News(String nTitle, String nContent, Bitmap nImage){
        title = nTitle;
        content = nContent;
        image = nImage;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);
    final AsyncTask task = new DatabaseConnection(this, Method.GET_ALL_NEWS).execute();
    try {
        task.wait();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public final void fillListView(List<News> news){
    recentNews = news;
    if(recentNews != null && !recentNews.isEmpty()){
        ((ListView)findViewById(R.id.lvNews)).setOnItemClickListener(this);
        final int size = recentNews.size();
        final String newsTitles[] = new String[size];
        for(int i=0; i<size; ++i)
            newsTitles[i] = recentNews.get(i).title;

        ((ListView)findViewById(R.id.lvNews)).setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsTitles));
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final News selectedNews = recentNews.get(position);
    startActivity(new Intent(this, ANewsActivity.class)
        .putExtra("title", selectedNews.title)
        .putExtra("content", selectedNews.content)
        .putExtra("image", BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)));

}

我的AsyncTask代码相关部分是这样的:

public DatabaseConnection(Context nContext, Method nMethod){
    method = nMethod;
    context = nContext;
}

@Override
protected void onPreExecute() {
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage(context.getString(R.string.database_connection_wait_message));
    progressDialog.setTitle(R.string.database_connection_wait_title);
    progressDialog.show();
}

@SuppressWarnings("incomplete-switch")
@Override
protected Void doInBackground(String... params) {
    if(method != Method.NONE){
        open();
        try{
            switch(method){
            case GET_ALL_NEWS:
                final ResultSet rs = conn.createStatement().executeQuery("select baslik, metin, resim from haberler");
                news =  new ArrayList<News>();
                while(rs.next())
                    news.add(new News(rs.getString(1), rs.getString(2), BitmapFactory.decodeStream(rs.getBlob(3).getBinaryStream())));
                break;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }
    return null;
}

@SuppressWarnings("incomplete-switch")
@Override
protected void onPostExecute(Void temp) {
    if (progressDialog.isShowing()){
        progressDialog.dismiss();
        switch(method){
        case GET_ALL_NEWS:
            ((NewsActivity)context).fillListView(news);
            break;
        }
        method = Method.NONE;
    }
}

我希望 UI 线程等待数据库操作完成。 顺便说一下,变量等没有初始化问题,数据库 returns 正确信息和我的 "news" 变量正常填充。

顺便说一句,我再次意识到它在手机上工作,有趣的是在模拟器上卡住了(如果我在主线程代码上删除 wait() 方法及其 try-catch 块)。

如果没有 logcat 输出,很难说是什么崩溃了,但它很可能是应用程序的主线程,因为您在 [=11= 中调用了 .wait() 方法].您的 onCreate() 不能等待 - 它必须初始化并退出,否则您将阻塞应用程序的主线程并破坏 AsyncTask.

的目的