Note that there are some explanatory texts on larger screens.

plurals
  1. POException "Can't create handler inside thread that has not called looper prepare" in android app
    text
    copied!<p>I'm working on my first android app and I'm trying to use Quickblox.com as my backend. </p> <p>In order to use it, I need to authorize the app by creating a session using their SDK.</p> <p>So, I have the following code: </p> <pre class="lang-java prettyprint-override"><code>// Initialize QuickBlox application with credentials. QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET); // Authorize application QBAuth.createSession(new QBCallback() { @Override public void onComplete(Result result) {} @Override public void onComplete(Result result, Object context) { if (result.isSuccess()) { showMainScreen(); } else { // print errors that came from server Toast.makeText(getBaseContext(), result.getErrors().get(0), Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.INVISIBLE); } } }, QBQueries.QB_QUERY_AUTHORIZE_APP); </code></pre> <p>This code works well with an emulator, but it doesn't work if I try with a real android phone. I have a connection timeout error. I think I need to make this kind of requests (Web Services) in the background right?</p> <p>So I tried to use the AsyncTask to make the request to QB in the background, and changed the code to this:</p> <pre class="lang-java prettyprint-override"><code>new AsyncTask&lt;Void, Void, Void&gt;() { @Override protected Void doInBackground(Void... params) { // Initialize QuickBlox application with credentials. QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET); // Authorize application QBAuth.createSession(new QBCallback() { @Override public void onComplete(Result result) {} @Override public void onComplete(Result result, Object context) { if (result.isSuccess()) { showMainScreen(); } else { // print errors that came from server Toast.makeText(getBaseContext(), result.getErrors().get(0), Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.INVISIBLE); } } }, QBQueries.QB_QUERY_AUTHORIZE_APP); return null; } }.execute(); </code></pre> <p>I've seen a lot of similar questions here at SO, but I can't seem to find an answer that works with my code. I saw that functions that deal with the UI need to be called from the main thread, so I suppose that the code I have inside</p> <pre class="lang-java prettyprint-override"><code>onComplete(Result result, Object context) </code></pre> <p>should be inside a block like this right? </p> <pre class="lang-java prettyprint-override"><code>runOnUiThread(new Runnable() { public void run() { // code here } }); </code></pre> <p>But I tried that as well and it didn't work. Any guesses?</p> <p>I believe the problem is not because of the Toast and showMainScreen(). It still fails with this code: </p> <pre class="lang-java prettyprint-override"><code>// Initialize QuickBlox application with credentials. QBSettings.getInstance().fastConfigInit(Consts.APP_ID, Consts.AUTH_KEY, Consts.AUTH_SECRET); QBAuth.createSession(new QBCallback() { @Override public void onComplete(Result arg0, Object arg1) { // TODO Auto-generated method stub } @Override public void onComplete(Result arg0) { // TODO Auto-generated method stub } }, QBQueries.QB_QUERY_AUTHORIZE_APP); </code></pre> <p>But it doesn't fail if I just create the QBCallback object, without passing it to the QBAuth.createSession function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload