【发布时间】:2015-11-16 04:33:15
【问题描述】:
对于我的 android 应用程序,我必须定义一个包含三个空白 TextView 的特定布局。接下来我要修改这些 TextView 的内容,然后以编程方式将此 LinearLayout 附加到另一个 LinearLayout。我的问题是,当我尝试在内部布局中引用 TextView 时,出现 NULL 指针异常。
这是我的内部布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="horizontal"
android:weightSum="3"
android:id="@+id/firstRowLinearLayout">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textStyle="bold"
android:id="@+id/matchTextView"
android:background="#ffffff"
android:textColor="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:textStyle="bold"
android:id="@+id/resultTextView"
android:background="#ffffff"
android:textColor="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textStyle="bold"
android:id="@+id/quoteTextView"
android:background="#ffffff"
android:textColor="#000000" />
</LinearLayout>
这是我尝试访问此布局内的 TextView 的代码:
LinearLayout couponLinearLayout = (LinearLayout) findViewById(R.id.couponLinearLayout); // outer layout
LinearLayout test = (LinearLayout) findViewById (R.id.firstRowLinearLayout); // layout to be added to the outer one
TextView first_field = (TextView) test.findViewById(R.id.matchTextView);
(R.id.resultTextView);
first_field.setText("TEST");
couponLinearLayout.addView(test);
使用此代码,正如我所说,当我定义 TextView 时出现 NULL 指针异常。希望你能帮助我!
【问题讨论】:
-
你的
couponLinearLayoutid 在哪里? -
你在夸大你的布局吗?
标签: java android layout reference textview