Steven Lignos
Steven Lignos

Reputation: 237

Azure mobile service, application crashes (offline synchronization - Android)

I'm trying to run the Android version of TodoOffine azure mobile service sample but when push and pull commands executes, my application is crashing. Does anyone understand this problem?

this is the code from sample:

new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    mClient.getSyncContext().push().get();
                    mToDoTable.pull(mPullQuery).get();
                    refreshItemsFromTable();
                } catch (Exception exception) {
                    createAndShowDialog(exception, "Error");
                }
                return null;
            }

        }.execute();

logcat:

10-29 13:31:16.448  25390-25478/com.example.blog20140807 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
Process: com.example.blog20140807, PID: 25390
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.app.Dialog.<init>(Dialog.java:109)
        at android.app.AlertDialog.<init>(AlertDialog.java:114)
        at android.app.AlertDialog$Builder.create(AlertDialog.java:931)
        at com.example.blog20140807.ToDoActivity.createAndShowDialog(ToDoActivity.java:343)
        at com.example.blog20140807.ToDoActivity.createAndShowDialog(ToDoActivity.java:327)
        at com.example.blog20140807.ToDoActivity.access$700(ToDoActivity.java:44)
        at com.example.blog20140807.ToDoActivity$2.doInBackground(ToDoActivity.java:197)
        at com.example.blog20140807.ToDoActivity$2.doInBackground(ToDoActivity.java:188)

Upvotes: 0

Views: 368

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87218

Try wrapping the call to builder.create().show(); inside the createAndShowDialog method in a runnable that goes to the UI thread:

private void createAndShowDialog(String message, String title) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(message);
            builder.setTitle(title);
            builder.create().show();
        }
    });
}

Upvotes: 2

Related Questions