Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • IllegalStateException not on main thread

    • 0
    • 0
    • 0
    • 3
    • 0
    • 1
    • 0
    • 608
    Answer it
                class LoadImage extends AsyncTask<Void, Void, Void> {
                                 @Override
                                 protected Void doInBackground(Void... urls) {
                                    Bitmap theBitmap = null;
                                                Log.e("","Wow It has to work");
                                                try {
                                                    theBitmap = Glide.
                                                            with(getActivity()).
                                                            load("https://graph.facebook.com/998365793528829/picture?type=large").
                                                            asBitmap().
                                                            into(100, 100). // Width and height
                                                            get();
                                                    BitmapDescriptor iconBitmap = BitmapDescriptorFactory.fromBitmap(theBitmap);
                                                    map.addMarker(new MarkerOptions()
                                                    .icon(iconBitmap)
                                                    .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
                                                    .position(new LatLng(19.129980, 72.834795)));
    
    
                                                } catch (InterruptedException | ExecutionException e) {
                                                    // TODO Auto-generated catch block
                                                    e.printStackTrace();
                                                }
           return null;
                                 }
                                 @Override
                                 protected void onPostExecute(Void result) {
                                 }
                         }
                    LoadImage li = new LoadImage();
                    li.execute();
    
    IllegalStateException not on main thread

 3 Answer(s)

  • Hello,

    Please check the following code and replace it with yours

    class LoadImage extends AsyncTask<Void, Void, Void> {
            @Override
            protected Void doInBackground(Void... urls) {
                Bitmap theBitmap = null;
                Log.e("", "Wow It has to work");
                try {
                    theBitmap = Glide.
                            with(act).
                            load("https://graph.facebook.com/998365793528829/picture?type=large").
                            asBitmap().
                            into(100, 100). // Width and height
                            get();
                    BitmapDescriptor iconBitmap = BitmapDescriptorFactory.fromBitmap(theBitmap);
    //                map.addMarker(new MarkerOptions()
    //                        .icon(iconBitmap)
    //                        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
    //                        .position(new LatLng(19.129980, 72.834795)));
                    final Bitmap finalTheBitmap = theBitmap;
                    act.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            map.addMarker(new MarkerOptions()
                                    .position(new LatLng(19.129980, 72.834795)))
                                    .title("Melbourne")
                                    .snippet("Population: 4,137,400")
                                    .icon(BitmapDescriptorFactory.fromBitmap(finalTheBitmap));
                        }
                    });
    
    
                } catch (Exception e) {
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
    
            }
        }
    

    If this answer solved your problem mark it correct .

    Happy Coding :) :)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: