0

ISSUE:

I'm getting an error in eclipse stating `Cannot make a static reference to the non-static method setImageBitmap(Bitmap) from the type ImageView

SOURCE:

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    public static final String VIDEO3_ID = "83ZgtqTw-mI";
    public static final String VIDEO4_ID = "n5wMza29JwI";
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;
    private YouTubeThumbnailView youTubeThumbnailView3;
    private YouTubeThumbnailView youTubeThumbnailView4;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private DrawerLayout mDrawerLayout;
    private ListView drawerListView;
    private ListView mDrawerList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;
    Drawable selstation_up_btn; 
    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);

final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);


Drawable d = getResources().getDrawable(selstation_up_btn);
imageView.setImageBitmap(((BitmapDrawable) d).getBitmap());

CITATION / EXAMPLE SOURCE / INITIAL ISSUE:

Footer Image Will Not Size Correctly

EDIT (In Response to Ramaral)

public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    public static final String VIDEO1_ID = "xVHHJqntuXI";
    public static final String VIDEO2_ID = "YWteQj_q3Ro";
    public static final String VIDEO3_ID = "83ZgtqTw-mI";
    public static final String VIDEO4_ID = "n5wMza29JwI";
    private YouTubePlayer youTubePlayer;
    private YouTubePlayerView youTubePlayerView;
    private YouTubeThumbnailView youTubeThumbnailView1;
    private YouTubeThumbnailView youTubeThumbnailView2;
    private YouTubeThumbnailView youTubeThumbnailView3;
    private YouTubeThumbnailView youTubeThumbnailView4;
    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private DrawerLayout mDrawerLayout;
    private ListView drawerListView;
    private ListView mDrawerList;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private ActionBarDrawerToggle mDrawerToggle;
    Drawable selstation_up_btn; 
    ImageView imageView;
    private YouTubeThumbnailLoader youTubeThumbnailLoader;
    ScrollView mainScrollView;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);

final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);


ImageView myImageView = (ImageView) findViewById(R.id.selstation_up_btn);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);  

ISSUE AFTER EDIT (In Response to Ramaral)

12-02 10:28:23.619: E/AndroidRuntime(24278): FATAL EXCEPTION: main
12-02 10:28:23.619: E/AndroidRuntime(24278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idg2/com.idg.omv.Home}: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.os.Looper.loop(Looper.java:137)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread.main(ActivityThread.java:4950)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at java.lang.reflect.Method.invokeNative(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at java.lang.reflect.Method.invoke(Method.java:511)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at dalvik.system.NativeStart.main(Native Method)
12-02 10:28:23.619: E/AndroidRuntime(24278): Caused by: java.lang.NullPointerException
12-02 10:28:23.619: E/AndroidRuntime(24278):    at com.idg.omv.Home.onCreate(Home.java:89)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.Activity.performCreate(Activity.java:5179)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-02 10:28:23.619: E/AndroidRuntime(24278):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-02 10:28:23.619: E/AndroidRuntime(24278):    ... 11 more
Community
  • 1
  • 1
user3009687
  • 369
  • 2
  • 7
  • 17

3 Answers3

2

This is because setImageBitmap seems not to be a static method but you call it like one. You probably need an instance of ImageView and call setImageBitmap on that instance.

I.e. you need to get the instance of the ImageView from your view and call then call the method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ImageView imgView = (ImageView) findViewById(R.id.the_id);
    imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());
}

You could also use the setImageDrawable method which might be more appropriate in your case.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    ImageView imgView = (ImageView) findViewById(R.id.the_id);
    imgView.setImageDrawable(d);
}

Since your error occurs because the drawable is null try to change this

Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(((BitmapDrawable) d).getBitmap());

to this

Bitmap d = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.selstation_up_btn);
ImageView imgView = (ImageView) findViewById(R.id.the_id);
imgView.setImageBitmap(d);
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
  • Is there a better way to call it? I'm simply trying to resize my footer image correctly: http://stackoverflow.com/questions/20229433/footer-image-will-not-size-correctly – user3009687 Dec 02 '13 at 15:07
  • try `ImageView imgView = (ImageView) findViewById(R.id.whatever_id_your_imageview_has)` and then you can call `imgView.setImageBitmap(((BitmapDrawable) d).getBitmap())` – mvieghofer Dec 02 '13 at 15:10
  • I did and it seems to leave me with the error The field YouTubeBaseActivity.d is not visible – user3009687 Dec 02 '13 at 15:18
  • maybe you should use the setImageDrawable method? – mvieghofer Dec 02 '13 at 15:26
  • Strange... actually you should have one since this method was added with API level 1, so it is there since always... maybe you can try it the code suggested in this post http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap – mvieghofer Dec 02 '13 at 15:34
  • I just saw you've updated your question... can you please mark the place where the NullPointerException occurs? Where is line 89? – mvieghofer Dec 02 '13 at 15:35
  • Shouldn't there be a simpler way of getting my footer image to resize correctly: http://stackoverflow.com/questions/20229433/footer-image-will-not-size-correctly – user3009687 Dec 02 '13 at 15:36
  • Line 89 is imageView.setImageDrawable(d); – user3009687 Dec 02 '13 at 15:36
  • So it seems that d is null at this point... This means that there is an error with loading the image. – mvieghofer Dec 02 '13 at 15:38
  • Should I leave imageView.setImageDrawable(d); below it? – user3009687 Dec 02 '13 at 15:42
  • when you use the code snippet i posted above to load a bitmap you need to use the setImageBitmap method to set the bitmap for the image view. – mvieghofer Dec 02 '13 at 15:44
  • That simply brings back the The field YouTubeBaseActivity.d is not visible error – user3009687 Dec 02 '13 at 15:51
  • do you have a field called `d` in your activity? – mvieghofer Dec 02 '13 at 15:54
  • are you using eclipse? if so, try to clean your project or restart eclipse because this is a really strange error... sometimes eclipse acts really weird and a clean or restart solves the problem... – mvieghofer Dec 02 '13 at 16:48
1

Your setImageBitmap() method is not static but you are trying to call it in a static way. That's the problem.

Try

reference.setImageBitmap(((BitmapDrawable) d).getBitmap()); // if you have a reference to it

or

setImageBitmap(((BitmapDrawable) d).getBitmap()); // if it is in your current class
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1

If your ImageView is in R.layout.home.
Try this

ImageView myImageView = (ImageView) findViewById(R.id.your_image_id);
myImageView.setImageBitmap(((BitmapDrawable) d).getBitmap());  

Edit

imageView = (ImageView) findViewById(R.id.your_image_id);
Drawable d = getResources().getDrawable(R.drawable.selstation_up_btn);
imageView.setImageDrawable(d);  

And selstation_up_btn must be in drawable folder.

ramaral
  • 6,149
  • 4
  • 34
  • 57
  • I have my ImageView in R.layout.home but I'm getting The field YouTubeBaseActivity.d is not visible selstation_up_btn cannot be resolved or is not a field when replacing the two lines Drawable d = getResources().getDrawable(selstation_up_btn); imageView.setImageBitmap(((BitmapDrawable) d).getBitmap()); – user3009687 Dec 02 '13 at 15:03
  • I updated my source as suggested (shown above) but now I'm getting a force close error (shown above) – user3009687 Dec 02 '13 at 15:31
  • @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } – user3009687 Dec 02 '13 at 15:52
  • You get a `NullPointerException` at `onCreate`. From logCat i can´t see where line 89 is. Is `R.id.view_pager` in `R.layout.home` ? – ramaral Dec 02 '13 at 16:00
  • Change `imageView.setImageDrawable(d);` to `myImageView.setImageDrawable(d);` or change `ImageView myImageView = (ImageView) findViewById(R.id.your_image_id);` to `imageView = (ImageView) findViewById(R.id.your_image_id);` – ramaral Dec 02 '13 at 16:35