2

簡単な質問ですが、わかりません。

これは私のxmlです:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

<GG.My_pic.testA
        android:id = "@+id/myview"
        android:layout_width = "fill_parent"
        android:layout_height = "fill_parent"
    />

</FrameLayout>

月着陸船では、メインスレッドが使用します

    // tell system to use the layout defined in our XML file
    setContentView(R.layout.lunar_layout);

しかし、私は私のものを使用することはできません

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

R.layout.main を --> new testA(this) に変更すると、動作します

(testA は SurfaceView を拡張したクラスで、SurfaceHolder.Callback を実装しています)

どうして??

4

1 に答える 1

5

原因はわかったのですが、原因がわかりません。

Java と android の両方の真の初心者である私は、調べるのに非常に長い時間を費やしました。

この問題の鍵は、

class gameView extends SurfaceView implementation SurfaceHolder.Callback {

public gameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

}

すべてのチュートリアルで、これがサーフェスビューの基本であることがわかります

そのような

http://android-er.blogspot.com/2010/05/android-surfaceview.html

http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html

surfaceview には 3 種類のコンストラクターがあります。

SurfaceView(Context context)
SurfaceView(Context context, AttributeSet attrs)
SurfaceView(Context context, AttributeSet attrs, int defStyle)

私は最初のものを使って一日を過ごしました:

SurfaceView(Context context)

そして、それは常に「強制終了」になります。

しかし、2番目のコンストラクターに目を向けると:

SurfaceView(Context context, AttributeSet attrs)

いきなり効く!

これが解決策です。

誰でも理由を教えてもらえますか??

于 2010-10-14T16:04:02.180 に答える