0

My program crashs after doInBackground and doesn't come to onPostExecute.

My activity code's related parts are like this:

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

}

My AsyncTask code's related parts are like this:

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

I want UI thread waits until database operations finishes. By the way there is no initialization problem at variables etc and database returns proper infos and my "news" variable is filled normally.

By the way again I realized it is WORKING on PHONE, STUCKS on EMULATOR interestingly (if I remove wait() method and its try-catch block on main thread code).

Oğuzhan Türk
  • 360
  • 2
  • 9
  • 21

1 Answers1

2

It's difficult to say what is crashing without the logcat output, but it would most likely be the main thread of the app because of the .wait() method you are calling in onCreate(). Your onCreate() cannot wait - it must initialize and exit, otherwise you are blocking the main thread of your app and defeating the purpose of the AsyncTask.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • If I remove wait() method with its try-catch block, then program stucks in open() method on database. LogCat outputs are like that "01-02 20:30:01.900: I/Choreographer(1772): Skipped 31 frames! The application may be doing too much work on its main thread. " Messages like that are recieved nonstop and ProgressDialog is running on screen. – Oğuzhan Türk Jan 02 '15 at 20:32
  • That points to another problem with your database being opened or manipulated on the main thread. – Larry Schiefer Jan 02 '15 at 21:22
  • There is no code to call open() remote database method from main thread directly. Removing wait() and running app on the phone solved my problem as I mentioned. But I still wonder why emulator fails. – Oğuzhan Türk Jan 02 '15 at 21:50
  • There should be no difference from the emulator side. You mentioned the database being remote; what do you mean by this? Is it wrapped by a `ContentProvider` or are you using some other type of mechanism to wrap the database? If you are using something over a network link, the emulator will have difference connectivity than a real device. Have you tried stepping over the `open()` call in the emulator? – Larry Schiefer Jan 02 '15 at 21:54
  • Remote means it isn't SQLite, it is MySql on a remote server. Program uses Internet to access it. I tried to step over but it didn't work. – Oğuzhan Türk Jan 02 '15 at 22:35
  • http://stackoverflow.com/questions/2206822/no-internet-on-android-emulator-why-and-how-to-fix/11742086#11742086 Solves the Internet connection problem. – Oğuzhan Türk Jan 25 '15 at 17:52