9

在 android 4.2 我的应用程序运行良好,但是当我尝试使用 android 4.0.4 对其进行测试时,我收到以下消息错误:

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)

我使用 setBackgroud 添加一些动画,比如这段代码

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));

我的项目中添加了支持库,但我不知道如何解决这个问题,任何帮助都将不胜感激!

4

4 回答 4

32

这是在API 16中添加的,试试setBackgroundDrawable()

于 2013-04-16T13:23:13.780 回答
31

除了@dmon 回答:您可以通过这种方式调用正确的方法:

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

    layout.setBackground(animation);

} else {

    layout.setBackgroundDrawable(animation);
}
于 2013-04-16T13:26:59.833 回答
2

试试这个,它对我有用:

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);

于 2014-12-08T07:27:26.910 回答
0

这是我的问题:

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

我把它修好了:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    imageButton.setOnTouchListener(popupWindow.createDragToOpenListener(imageButton));
                }
于 2020-10-03T10:58:38.430 回答