1

私はアンドロイドが初めてで、助けていただければ幸いです。背景をビューとして使用しようとしています。次のファイルを含む簡単なクレジット ページを作成するとします: credits.xml Credits.java

Credits.java:

public class Credits extends Activity implements OnTouchListener
{   
    private FrameLayout fl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            fl = (FrameLayout)findViewById(R.id.credits_screen);
            if ( fl == null ) Log.e("Credits", "fl is null");
            else {
                  fl.setOnTouchListener(this);
                  setContentView(fl);
            }
    }
    @Override 
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}

私の credits.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"
 android:focusable="true" android:id="@+id/credits_screen">

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:src="@drawable/credits" android:scaleType="fitXY" />

</FrameLayout>

どうやら fl は (FrameLayout)findViewById() で null を返すようです。R.id.credits_screen が R ファイルで生成されていることがわかります。私のコードの何が問題なのかを知ることができますか? FrameLayout を見つけられないのはなぜですか?

4

1 に答える 1

3

findViewById() の前に setContentView() を呼び出す必要があります。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.credits);
    fl = (FrameLayout) findViewById(R.id.credits_screen);
    fl.setOnTouchListener(this);
}
于 2013-01-24T21:30:13.623 に答える