使用FrameView android在layout.xml中覆盖UI元素



我在mail.xml布局中添加了一个文本框。我也有一个用于绘制游戏的表面视图。我正试图使用FrameView将main.xml布局中的文本框覆盖到SurfaceView上,但我遇到了一个null错误。

我在创建时,一切都很好,直到我调用"f.addView(text);"

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    iso = new Iso(30,30, this);
    FrameLayout f = new FrameLayout(this);
    f.addView(iso);
    text = (EditText) findViewById(R.id.txt);
    f.addView(text);
    setContentView(f);
}

我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10" >
    <requestFocus />
</EditText>

我最终得到了一个空指针异常,如果我去掉"f.addView(text)"这一行,它会很好地工作,请帮助

您忘记了扩展xml布局。

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   iso = new Iso(30,30, this);
   FrameLayout f = new FrameLayout(this);
   f.addView(iso);
   View rootView = LayoutInflater.from(this).inflate(R.layout.main, f);
   text = (EditText) rootView.findViewById(R.id.txt);
   f.addView(text);
   setContentView(f);
}

最新更新