0

I am getting following exception at line 1414 from a fragment.

java.lang.IllegalStateException: Fragment HomeFragment{2104037} not attached to Activity
                                                                               at android.support.v4.app.Fragment.getResources(Fragment.java:715)
                                                                               at com.juarezserver.citystatus.fragment.HomeFragment.addMarker(HomeFragment.java:1414)

This is the piece of code involved:

if (tiporeporte.equals("1")){

            int height = 75;
            int width = 75;
            BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.tipo_1);//line 1414
            Bitmap b=bitmapdraw.getBitmap();
            Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);


            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
        }

I don´t know what am I doing wrong.

mvasco
  • 4,965
  • 7
  • 59
  • 120

3 Answers3

0

I believe the problem is in:

BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.tipo_1);

where you try to access the resources through getResources() and your fragment is not attached yet.

Try to call that code block in your Fragment's onAttache() or any other lifecycle event where the Fragment's Activity is ready.

Please check the Fragment's lifecycle: https://developer.android.com/guide/components/fragments.html

Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
  • 1
    The problem is at the line itself. I have removed it and changed the way to get the drawable here : markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.tipo_1)); and now it works without issues.Thank you – mvasco Dec 18 '17 at 10:47
0

You are trying to call
(BitmapDrawable)getResources().getDrawable(R.drawable.tipo_1);//line 1414 before fragment has been attached to an Activity. First wait for Fragment to Attach an activity and then you can call yourConvertView.getResources().*** Where yourConvertView is a View which you got on OnCreateView() of Fragment


Update
This may help Someone.
Replaced

(BitmapDrawable)getResources().getDrawable(R.drawable.tipo_1);//line 1414

With

markerOptions.icon(BitmapDescriptorFactory.fromResource(R.dr‌​awable.tipo_1));

and it worked

GreenROBO
  • 4,725
  • 4
  • 23
  • 43
  • I am not sure, but I guess you are not right. The problem is not happening always, if you were right, the issue should always appear. – mvasco Dec 18 '17 at 10:34
  • I went through the same behavior once and later I found this reason. – GreenROBO Dec 18 '17 at 10:37
  • OK then, but I don´t know what to do with your proposal. – mvasco Dec 18 '17 at 10:39
  • The problem is at the line itself. I have removed it and changed the way to get the drawable here : markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.tipo_1)); and now it works without issues.Thank you – mvasco Dec 18 '17 at 10:47
  • Thank you for informing back! – GreenROBO Dec 18 '17 at 11:34
0

I have tried changing the way to get the drawable, and the problem is gone.

if (tiporeporte.equals("1")){

            markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.tipo_1));
        }
mvasco
  • 4,965
  • 7
  • 59
  • 120