我在文件夾「layout /」中有一個名爲「debug.xml」的自定義佈局,它需要以編程方式添加到名爲centerLayout的activity_main.xml中的預定義佈局中。該 「debug.xml」 是一樣的東西:以編程方式添加布局資源
<?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:id="@+id/debug_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/commandEditText"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:inputType="text" />
</LinearLayout>
</LinearLayout>
我要添加這個 「debug_layout」 成centerLayout:
LinearLayout centerLayout = (LinearLayout) findViewById (R.id.center_layout);
LinearLayout debugLayout = (LinearLayout) findViewById (R.id.debug_layout);
centerLayout.addView(debugLayout);
但它在「centerLayout.addView(debugLayout)遇到NULL指針EXCETION ;」。所以看來debug_layout沒有被初始化或者什麼。有人可以幫助我嗎?