Android 将文本视图添加到FrameLayout中的某个位置

Android 将文本视图添加到FrameLayout中的某个位置,android,textview,android-framelayout,Android,Textview,Android Framelayout,创建框架布局后,我想将TextView添加到特定位置。但当我尝试添加视图时,应用程序崩溃。怎么了 我的代码: float metrics; TextView text, text2; FrameLayout screen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView

创建框架布局后,我想将TextView添加到特定位置。但当我尝试添加视图时,应用程序崩溃。怎么了

我的代码:

float metrics;
TextView text, text2;
FrameLayout screen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        screen = (FrameLayout)findViewById(R.id.screen);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        text = (TextView)findViewById(R.id.text);
        text.setText(""+metrics + "\n"+metrics.widthPixels + "\n"+ metrics.heightPixels);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(100, 100);
        text.setText("XXX");
        screen.addView(text2, 100, 100);
下面是我的XML代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/a"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

你的应用程序崩溃,因为你没有在任何地方初始化
text2

也许您想创建一个新的
文本视图

text2 = new TextView(this);

可以添加堆栈跟踪和layout.xml吗?除非我们看到这一点,否则很难弄清楚到底发生了什么。在这里发布您的日志。同时发布您的xml文件。非常感谢,但不要忘记将回答标记为已回答,因此在进一步的问题中,人们可能会花时间回答他们。。。
text2 = new TextView(this);