我正在尝试扩展RelativeLayout对象并包含一个嵌入式SurfaceView对象,该对象使用我/ res / drawable文件夹中的png文件作为其背景但我在XML Layout编辑器中不断收到错误。请参阅以下代码:
public class StopMotionRelativeLayout extends RelativeLayout
{
private Context myContext;
private SurfaceView surfaceView1;
private BitmapFactory.Options myBitmapOptions;
private final android.view.ViewGroup.LayoutParams params =
new LayoutParams(LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT);
public StopMotionRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
myContext = context;
this.setBackgroundColor(Color.GREEN);
//init images
surfaceView1 = new SurfaceView(myContext,attrs);
surfaceView1.setVisibility(View.INVISIBLE);
this.addView(surfaceView1, params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
myBitmapOptions = new Options();
myBitmapOptions.outWidth = widthMeasureSpec;
myBitmapOptions.outHeight = heightMeasureSpec;
surfaceView1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(this.myContext.getResources(),
R.drawable.golf1, myBitmapOptions)));
}
}
我收到以下错误:
NotFoundException:找不到 可绘制的资源匹配值 0x7F020002(已解析名称:golf1)in 目前的配置。
我现在已经看到过这种类型的错误,当我尝试通过代码而不是XML从资源文件加载某些东西时,它总会发生。奇怪的是,这个错误并没有阻止我编译应用程序,并且应用程序在模拟器中运行时没有错误。我想恢复使用我的布局编辑器......
请帮忙。
更新:这是布局XML
<?xml version="1.0" encoding="utf-8"?>
<com.games.test.StopMotionRelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</com.games.test.StopMotionRelativeLayout>
答案 0 :(得分:0)
我想知道如果你在XML中为android:background指定了一个值,然后在代码中覆盖它,它是否会起作用。
作为一种解决方法,您还可以尝试调用isInEditMode()函数,而不是在编辑模式下设置背景。