NoSuchMethodError when I use android.widget.RelativeLayout.setBackground
Asked Answered
G

4

9

At android 4.2 my app works fine, but when I try to test it using android 4.0.4 I have this message error:

04-16 15:06:46.601: E/AndroidRuntime(3844): java.lang.NoSuchMethodError: android.widget.RelativeLayout.setBackground
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.myapp.MainActivity.cambiarPagina(MainActivity.java:271)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.myapp.MainActivity.cambiarPagina(MainActivity.java:119)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.myapp.MainActivity.access$0(MainActivity.java:116)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.myapp.MainActivity$2.onClick(MainActivity.java:70)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.view.View.performClick(View.java:3524)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.view.View$PerformClick.run(View.java:14202)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.os.Handler.handleCallback(Handler.java:605)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.os.Handler.dispatchMessage(Handler.java:92)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.os.Looper.loop(Looper.java:137)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at android.app.ActivityThread.main(ActivityThread.java:4499)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
    04-16 15:06:46.601: E/AndroidRuntime(3844):     at dalvik.system.NativeStart.main(Native Method)

I use setBackgroud to add some animation, like this code

AnimationDrawable animation = new AnimationDrawable();
                                 animation.addFrame(getResources().getDrawable(R.drawable.d1), 200);
                                 animation.addFrame(getResources().getDrawable(R.drawable.dio), 1000);
                                 animation.addFrame(getResources().getDrawable(R.drawable.da1), 200);
 animation.setOneShot(true);
 view.setBackground(animation);
 view.post(new Starter(animation));

I have support library added to my project, and I don't know how solve this, any help will be apreciated!

Genethlialogy answered 16/4, 2013 at 13:20 Comment(2)
What is there in MainActivity.java:271 ? What is the type of view in `view.setBackground(animation);' ?Wholesome
read the doc, setBackgroundDrawable does not exist before API 16.Palanquin
E
32

That was added in API 16, try setBackgroundDrawable()

Elbow answered 16/4, 2013 at 13:23 Comment(4)
Hi dmon, Thanks for your answer, If I use setBackgroudDrawable() eclipse say it's deprecatedGenethlialogy
That's fine, that's because they added the other method. It's interesting that they deprecated the other one given that they are not 100% compatible. Though granted, I did not appreciate how the old method worked :)Elbow
Thanks @Elbow ! It works for me! One question, I can detect android api level and only use the deprecated method at previous versions to api 16?Genethlialogy
why is now deprecatedUella
E
31

In addition to @dmon answer: you could call the proper method in this way:

if (Build.VERSION.SDK_INT >= 16) {

    layout.setBackground(animation);

} else {

    layout.setBackgroundDrawable(animation);
}
Embrasure answered 16/4, 2013 at 13:26 Comment(0)
P
2

Try this, it worked for me:

Bitmap bitmap = BitmapFactory.decodeFile(new ImageLoader().fullPath + "/desiredFilename.png");
Resources res = getResources();
BitmapDrawable bitmapDrawable = new BitmapDrawable(res, bitmap);
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN)
   ll.setBackgroundDrawable(bitmapDrawable);
else 
   ll.setBackground(bitmapDrawable);
Precarious answered 8/12, 2014 at 7:27 Comment(0)
C
0

this was my problem :

imageButton.setOnTouchListener(popupWindow.createDragToOpenListener(imageButton));

and I fixed it like this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    imageButton.setOnTouchListener(popupWindow.createDragToOpenListener(imageButton));
                }
Compatible answered 3/10, 2020 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.