1

RelativeLayout オブジェクトを拡張し、背景として /res/drawable フォルダー内の png ファイルを使用する埋め込み SurfaceView オブジェクトを含めようとしていますが、XML レイアウト エディターでエラーが発生し続けます。次のコードを参照してください。

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) に一致するドローアブル リソースが見つかりませんでした。

私はこのタイプのエラーを何度も見てきましたが、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>
4

1 に答える 1

0

XML で android:background の値を指定し、それをコードでオーバーライドするとうまくいくのだろうか。

回避策として、編集モードで背景を設定せずに isInEditMode() 関数を呼び出すこともできます。

于 2011-04-21T19:08:16.260 に答える