I am having problems executing a AlertDialog on a postexecute(). Throws this exception
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Alternatively, when I placed the AlertDialog.Builder in, it just didn't work
Pls help.
Also in a case were a wrong password is entered, the process is terminated. How can I call a Toast method in a case were the usename or password is invalid
Below is the code snippet
public void Login() {
// Toast.makeText(getBaseContext(), pass.getText() + " "
// +user.getText(),
// Toast.LENGTH_SHORT).show();
String url = "http://107.20.195.151/mcast_ws/" + "?user="
+ user.getText().toString() + "&password="
+ pass.getText().toString();
result = getHttpResponse(url);
}
String result;
private String getHttpResponse(String location) {
result = "";
URL url = null;
Log.d(LOGTAG, " " + "location " + location);
try {
url = new URL(location);
} catch (MalformedURLException e) {
Log.e(LOGTAG, " " + "error" + e.getMessage());
}
if (url != null) {
try {
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
int lineCount = 0;
while ((inputLine = in.readLine()) != null) {
result += inputLine.trim();
}
in.close();
connection.disconnect();
} catch (Exception e) {
Log.e(LOGTAG, " " + "IOError " + e.getMessage());
Toast.makeText(getBaseContext(), "No Internet Access",
Toast.LENGTH_SHORT);
}
} else {
Log.e(LOGTAG, " " + "url" + url);
}
return result;
}
class PostToTwitter extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
Login();
Log.d(LOGTAG, "Success");
Log.d(LOGTAG, result);
Log.d(LOGTAG, result.substring(0, 16).trim());
// Log.d(TweetActivity.getLogtag(),"Successfully Posted: " +
// params[0]);
return "success";
}
#Override
protected void onPostExecute(String r) {
// TODO Auto-generated method stub
super.onPostExecute(result);
String msg = "Login successful";
if (result.substring(0, 16).trim().equals(msg)) {
// System.out.println(result.substring(0, 16).trim());
Log.d(LOGTAG, " " + "Connection Test" + result);
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Are you sure send this SMS?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//...Attach another thread event to send the sms
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Log.e(LOGTAG, "Error detected 2");
AlertDialog alert = builder.create();
alert.show();
//return "success";
// Toast.makeText(getBaseContext(),
// "Login Succesful",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(),
"Login UnSuccesful. Check Username or password",
Toast.LENGTH_SHORT).show();
//return null;
}
// Toast.makeText(getApplicationContext(), result
// ,Toast.LENGTH_SHORT).show();
Log.e(LOGTAG, "Error detected");
/*
Intent i = new Intent("com.sms.subsahara.COMPOSESMS");
startActivity(i);
//Log.e(LOGTAG, " " + "error2");*/
}
}
On applying the suggestions from Alex, I modified the original code this above, but still get an error.Below is the exception from the logcat
E/AndroidRuntime( 326): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 326): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 326): at android.view.ViewRoot.setView(ViewRoot.java:472)
E/AndroidRuntime( 326): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 326): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 326): at android.app.Dialog.show(Dialog.java:239)
E/AndroidRuntime( 326): at com.sms.subsahara.WebMessengerActivity$PostToTwitter.onPostExecute(WebMessengerActivity.java:216)
E/AndroidRuntime( 326): at com.sms.subsahara.WebMessengerActivity$PostToTwitter.onPostExecute(WebMessengerActivity.java:1)
E/AndroidRuntime( 326): at android.os.AsyncTask.finish(AsyncTask.java:417)
E/AndroidRuntime( 326): at android.os.AsyncTask.access$300(AsyncTask.java:127)
E/AndroidRuntime( 326): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
E/AndroidRuntime( 326): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 326): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 326): at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime( 326): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 326): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime( 326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime( 326): at dalvik.system.NativeStart.main(Native Method)
doInBackground is not synchronized with the UI thread, which means you cannot directly manipulate UI elements, launch dialogs, etc. from within the method. The fix is easy. Simply move your AlertDialog code to the onPostExecute method (which is synchronized with the UI thread).
When working with AsyncTasks, remember that:
doInBackground is meant to perform the potentially expensive operation (network access, socket connection, database query, etc.)
onPostExecute is meant to do something with the results, if you wish (this method is synchronized with UI thread, so you can manipulate UI elements directly)
DoInBackground() only gets executed on a different thread than the main UI thread. That is the whole point of the AsyncTask.
There are 3 ways to post your results to the UI (pretty much... to the UI Thread)
use onPostExecute method. You get the return value of your doInBackground method passed in, and you can do anything with it. (I think this matches your use case.)
If your task is on going, and you want little bursts of information on the UI, like a progress bar update, use publishProgress(...) from doInBackground method, which will then be passed in to your onProgressUpdate(...) method in AsyncTask, which will run on the UI Thread.
Similar to 2, but you can use RunOnUiThread(...) convenience method to throw a runnable to run in the UI Thread. May not be the prettiest code when you have multiple anonymous methods floating around, but it's for quick and dirty way to do it. NOTE that this method is available on the Activity Class, and not Context class, which may be a deal breaker for some scenarios.
I guess it's because you Login () is in AsyncTask inside the execution, not in the mainUI inside execution. Can onPostExecute handler out in inside, back to the main thread execution; Also can use in onPostExecute UIThread:
runOnUiThread(new Runnable() {
#Override
public void run() {
Login();
}
});
I hope it can help you.
#Alex Lockwood
AsyncTask.doInBackground(Void..params) is ill suited for very long and repetitive operations, the better replacement would be a HandlerThread.
Reason:
In android 3.2, AsyncTask changed its implementation in a significant way. Starting in Android 3.2, AsyncTask does not create a thread for each instance in AsyncTask, instead it uses an executor to run background work work for all AsyncTasks on a single background thread! That means that each AsyncTask thread must run one after another. So, for example, if you have a long AsyncTask thread (image fetcher from a REST), I suggest using a HandlerThread instead, because the long task running thread could probably delay the whole chain.
BUT
It is possible to safely run AsyncTask in parallel by using a thread pool executor instead, but I don't know the exact reason, programmers don't recommend doing so, there are couple of reasons behind that which is not relevant to this post. But in case you do this, I would suggest you do your own threading instead, using handlers to communicate to the main thread when necessary.
Related
I have successfully sent an email in the background and displayed a progress dialog to the user as said email is sent. However when the user flips the screen while the dialog is up I get an IllegalArgumentException. I have tried using a WeakReference object and it doesn't seem to be fixing the problem. This is my AsyncTask class.
private class SendMailTask extends AsyncTask<Mail, String, EmailStatusResponce> {
private final WeakReference<ProgressDialog> weakReference;
public SendMailTask() {
ProgressDialog progressDialog = new ProgressDialog(SendReportActivity.this);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
weakReference = new WeakReference<>(progressDialog);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected EmailStatusResponce doInBackground(Mail... mail) {
return mail[0].send();
}
#Override
protected void onPostExecute(EmailStatusResponce result) {
super.onPostExecute(result);
if (result != null && weakReference != null) {
weakReference.get().dismiss(); // This is where the exception is thrown.
if (result.isSuccess()) {
Intent intent = new Intent(SendReportActivity.this, MainActivity.class);
startActivity(intent);
}
else {}
}
}
This is the exception
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{688d3e2 V.E...... R......D 0,0-1026,348} not attached to window manager
Thanks in advance for any and all help.
EDIT: More logcat
--------- beginning of crash
12-11 16:22:40.154 1976-1976/com.blazapps.allenfamilymedicine E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.blazapps.allenfamilymedicine, PID: 1976
java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{f89e667 V.E...... R......D 0,0-760,232} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
at android.app.Dialog.dismissDialog(Dialog.java:362)
at android.app.Dialog.dismiss(Dialog.java:345)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:168)
at com.blazapps.allenfamilymedicine.SendReportActivity$SendMailTask.onPostExecute(SendReportActivity.java:138)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I still do no know why the exception is being thrown but because I save lots of information in EmailStatusResponse I can just surround the exception with a try catch and if the task was complete I get the result I expected.
try {
weakReference.get().dismiss();
} catch (Exception e) {}
If anyone can figure a better solution I would really appreciate it. I hate just catching exceptions. There is usually a better way.
So I have an app that uses a surface view that runs a separate thread for the UI. It was taken almost directly from the Lunar Landing sample app. The app also uses Bluetooth service on another thread but I am sure that this is not related to the problem because I can disable bluetooth all together and it still happens.
The problem in my app is that the app when closed and then reopened does not start running the UI thread afterthread.start() except it throws an error. In the Lunar example they have thread.start() in the onSurfaceCreated method. The problem is when I restart my app (it calls onPause then onSurfaceDestroy) the thread is already running and I get an error when I try to start it. My code for onSurfaceCreated, onPause, onResume and onSurfaceDestroyed is all the same as the example. I know I can use if (this.getState() == Thread.State.NEW) { but that seems like it will mask some of my other issues. I want to master the activity life cycle.
My question is how does the Lunar Lander stop the thread? And why is mine not stopping with the same code and running at the onSurfaceCreated method. Obviously I am missing something. As far as I know in the Lunar example the only thing that is called on the thread on a destroy is thread.join().
Edit 3: Here is the Lunar Lander Example Code if needed.
So these are the three override methods in my surfaceview...
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
Log.d(TAG, "surfaceCreated");
// start the bluetooth service
thread.startBluetoothService();
// start the game
//if (this.getState() == Thread.State.NEW) {
Log.d(TAG, "thread start");
// start running the thread
this.start();
//}
Log.d(TAG, "running to true");
// release the thread lock
setRunning(true);
}
// surfaceChanged is called at least once after surfaceCreated
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG, "surfaceChanged");
// reset the surface size
thread.setSurfaceSize(width, height);
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
Log.d(TAG, "surfaceDestroyed");
// make sure to shut down the thread cleanly
boolean retry = true;
// stop the running thread
thread.setRunning(false);
// continuously try to shut down the thread
while (retry){
try{
// blocks calling thread until termination
thread.join();
// stop the bluetooth service
//thread.stopBluetoothService();
retry = false;
}catch(InterruptedException e){
//try to shut it down again
}
}
}
I am really pretty lost with all of this. Any help would be very appreciated, thanks!
Edit:
So I did a little more testing. When the user hits home(which exits the app completely) onPause, then onSurfaceDestroy like I said before. Then when it restarts I get onResume followed by onSurfaceCreated. I think my issue is that it is not calling onCreate when you reenter the app.
Some more questions...
What distinguishes the difference between a onPause and a onDestroy? I think my problem is that since onCreate is not being called I don't have a newly created UI thread which seems like it is still running.
Should the thread be stopped even on a onPause? Because then I am not garenteed to run onCreate which re instantiates the thread. Here is the onCreate code...
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wobble);
// get view and thread
wobbleView = (WobbleView) findViewById(R.id.wobble);
wobbleThread = wobbleView.getThread();
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
// alert the user of bluetooth failure
Toast.makeText(this, "Bluetooth is not available, using internal devices sensors", Toast.LENGTH_LONG).show();
// set the data source to internal sensors - so we'll just use the devices accel
wobbleThread.setDataSource(WobbleThread.INTERNAL_SENSORS);
// bluetooth is supported so make sure its enabled and
}else{
// make sure bluetooth is enabled on the device
if (!mBluetoothAdapter.isEnabled()) {
Log.d(TAG, "starting request to enable bluetooth");
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
// all is well with bluetooth - use bluetooth
Log.d(TAG, "setting bluetooth to bluetooth");
wobbleThread.setDataSource(WobbleThread.BLUETOOTH);
}
// give the LunarView a handle to the TextView used for messages
wobbleView.setTextView(
(TextView) findViewById(R.id.text_accel),
(TextView) findViewById(R.id.game_msg),
(TextView) findViewById(R.id.text_score),
(TextView) findViewById(R.id.bluetooth_status)
);
if (savedInstanceState == null) {
// we were just launched: set up a new game
//wobbleThread.setState(wobbleThread.STATE_READY);
} else {
//wobbleThread.setRunning(true);
// we are being restored: resume a previous game
//wobbleThread.restoreState(savedInstanceState);
}
}
Edit 2:
Some logcat output
So this is what I get when the thread.start() is called after reopening the app.
11-18 22:50:44.104 4868-4868/com.bme.shawn.wobble E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalThreadStateException: Thread already started
at java.lang.Thread.checkNotStarted(Thread.java:871)
at java.lang.Thread.start(Thread.java:1025)
at com.bme.shawn.wobble.WobbleThread.startGame(WobbleThread.java:213)
at com.bme.shawn.wobble.WobbleView.surfaceCreated(WobbleView.java:94)
at android.view.SurfaceView.updateWindow(SurfaceView.java:580)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:240)
at android.view.View.dispatchWindowVisibilityChanged(View.java:7903)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)
at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1071)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1289)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1050)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5750)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
And if I use the thread.getState check and bypass the error I get this when I close then reopen the app. (logs in basically everything) In this case when the app reopens the thread is not drawing at all. Which is very weird since without the check I get an error saying that the thread is already running.
11-18 22:59:13.444 5345-5345/com.bme.shawn.wobble D/WobbleActivity﹕ onPause
11-18 22:59:13.584 5345-5345/com.bme.shawn.wobble D/WobbleView﹕ surfaceDestroyed
11-18 22:59:17.794 5345-5345/com.bme.shawn.wobble D/WobbleActivity﹕ onResume
11-18 22:59:17.804 5345-5345/com.bme.shawn.wobble D/WobbleView﹕ surfaceCreated
11-18 22:59:17.804 5345-5345/com.bme.shawn.wobble D/WobbleThread﹕ running to true
11-18 22:59:17.804 5345-5345/com.bme.shawn.wobble D/WobbleView﹕ surfaceChanged
11-18 22:59:17.804 5345-5345/com.bme.shawn.wobble D/WobbleThread﹕ setting surface sizes
11-18 22:59:17.824 5345-5345/com.bme.shawn.wobble D/dalvikvm﹕ GC_FOR_ALLOC freed 3343K, 2% free 6585K/6668K, paused 13ms, total 13ms
11-18 22:59:17.844 5345-5345/com.bme.shawn.wobble E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 44
11-18 22:59:17.854 5345-5345/com.bme.shawn.wobble E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 50
Create a new instance of the thread in surfaceCreated() and start it. And call thread.join() in surfaceDestroyed()to destroy it.
I am developing my first android app. I have been created a Service class which role is to check if any new information on an external webpage. The HTTP request and service work as i should, but after a while I get these OutOfMemoryError.
Are someone able to see where the Service gather all that memory?
Error message 1.
java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again
at java.lang.VMThread.create(Native Method)
at java.lang.Thread.start(Thread.java:1029)
at org.apache.http.impl.conn.tsccm.AbstractConnPool.enableConnectionGC(AbstractConnPool.java:140)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.createConnectionPool(ThreadSafeClientConnManager.java:120)
at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.(ThreadSafeClientConnManager.java:98)
at com.loopj.android.http.AsyncHttpClient.(AsyncHttpClient.java:210)
at com.loopj.android.http.AsyncHttpClient.(AsyncHttpClient.java:149)
at com.loopj.android.http.AsyncHttpClient.(AsyncHttpClient.java:119)
at com.quickit.app.MyService.checkUpdates(MyService.java:89)
at com.quickit.app.MyService.access$1(MyService.java:75)
at com.quickit.app.MyService$TimeDisplayTimerTask$1.run(MyService.java:68)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
Error message 2.
java.lang.OutOfMemoryError: thread creation failed
at java.lang.VMThread.create(Native Method)
at java.lang.Thread.start(Thread.java:1050)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:913)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1295)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:81)
at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:893)
at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:688)
at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:671)
at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:658)
at com.quickit.app.MyService.checkUpdates(MyService.java:90)
at com.quickit.app.MyService.access$1(MyService.java:75)
at com.quickit.app.MyService$TimeDisplayTimerTask$1.run(MyService.java:68)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5092)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)
My service class.
public class MyService extends Service {
boolean login = false;
// constant
public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds
String address = Utilities.getAPIUrl();
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
SharedPreferences prefs = getSharedPreferences("com.quickit.app", Context.MODE_PRIVATE);
login = prefs.getBoolean("login", false);
// cancel if already existed
if(mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
public class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
if(login) {
checkUpdates();
}
}
});
}
}
private void checkUpdates() {
final SharedPreferences prefs = getSharedPreferences("com.quickit.app", Context.MODE_PRIVATE);
final String from_id = prefs.getInt("user", 0)+"";
final String lastCheck = prefs.getString("last_check", "0");
RequestParams params = new RequestParams();
params.put("type", "get_ask_questions");
params.put("fromid", from_id);
params.put("last_check", lastCheck);
AsyncHttpClient client = new AsyncHttpClient();
client.post(address, params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
try {
notification(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
Your code is constantly creating a new AsyncHttpClient object every time that timer expires! If the object never finishes its work, at some point, you will run out of memory.
Since you are just periodically checking for updates, you should make the AsyncHttpClient object static and just reuse it.
Look at http://loopj.com/android-async-http/ specifically, the Recommended Usage section.
Hi I am trying to execute the code below it suppose to open a loading dialog and dismiss it in the if statement.
here is the code:
loginBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
final ProgressDialog progress = ProgressDialog.show(thisActivity, "Please wait", "Loading please wait..", true);
Thread loginThread = new Thread(new Runnable()
{
#Override
public void run()
{
try
{
boolean userAllowed = login.loginUser(userEmail.getText().toString(), userPass.getText().toString());
if(userAllowed)
{
progress.dismiss();
startActivity(mainPage);
}
else
{
progress.dismiss();
Toast.makeText(context, "Invalide email and password", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(context, "There is some problem", Toast.LENGTH_LONG).show();
}
}
});
loginThread.start();
}
});
the logCat error output is:
05-09 22:37:26.508: E/AndroidRuntime(24820): FATAL EXCEPTION: Thread-1306
05-09 22:37:26.508: E/AndroidRuntime(24820): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.os.Handler.<init>(Handler.java:197)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.os.Handler.<init>(Handler.java:111)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast$TN.<init>(Toast.java:324)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast.<init>(Toast.java:91)
05-09 22:37:26.508: E/AndroidRuntime(24820): at android.widget.Toast.makeText(Toast.java:238)
05-09 22:37:26.508: E/AndroidRuntime(24820): at com.shale.activities.MainActivity$1$1.run(MainActivity.java:93)
05-09 22:37:26.508: E/AndroidRuntime(24820): at java.lang.Thread.run(Thread.java:856)
My reference was this tutorial.
Thanks!
as in Log :
RuntimeException: Can't create handler inside thread that has not
called Looper.prepare()
means you are trying to update or access UI elements from non ui Thread . so you will need to use Activity.runOnUiThread , Handler or AsyncTask for updating or accessing UI from other Thread. do it as:
Your_Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
//update Ui elements here
}
});
You can't do anything UI related (including dismissing dialogs) unless you're on the UI thread. You need to offload that via a runOnUiThread or by passing a message to a handler on the UI thread. Or better yet- make that thread an AsyncTask and do it in the onPostExecute()
An alternative to the above would be to send a broadcast and have a BroadcastReceiver handle receiving that broadcast and act upon it as you wish.
I am trying to create a thread to handle a login function that is executed when the login button is pushed, so that I can show a progressDialog.
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Check Login
ProgressDialog.show(Activity.this, "", "Loading...");
new Thread(new Runnable(){
public void run(){
try{
int duration;
Toast toast;
Context context;
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
JSONArray jsonArray=null;
password=ServerConnection.encryptPassword(password);
//Log.i("login hash",password);
jsonArray=ServerConnection.login(username, password);
if(jsonArray!=null) //login successful
{
context=getApplicationContext();
duration=Toast.LENGTH_SHORT;
toast=Toast.makeText(context, "Login Successful!", duration);
toast.show();//Shows the little pop up right at the bottom of the screen
Intent i = new Intent(getApplicationContext(), MapWithFriends.class);
startActivity(i);
}
else
{
context=getApplicationContext();
duration=Toast.LENGTH_SHORT;
toast=Toast.makeText(context, "Login Fail", duration);
toast.show();//Shows the little pop up right at the bottom of the screen
//lblResult.setText("Login failed. Username and/or password doesn't match.");
}
}catch(Exception e)
{
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}).start();
}
});
However when the thread is created, it force closes. If I change back to no threads, it works fine.
Thanks
Edit:
LogCat of crash:
Can't create handler inside thread that has not called Looper.prepare()
FATAL EXCEPTION: Thread-10
java.lang.NullPointerException
.....
would you guys need more than this?
You shouldn't modify the UI from any thread except the main thread , as you do in: progressDialog.dismiss();
Consider using Handler or AsyncTask instead. And I would also recommend reading this article.
From a non-UI thread use something like
runOnUiThread(new Runnable() {
// Your UI modifications here
});
or
yourUIControl.post(new Runnable() {
// something like
yourUIControl.setText("new text");
});