【发布时间】:2012-01-20 21:43:12
【问题描述】:
这是xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
这是Java代码:
View mainLayout = findViewById(R.id.mainLayout);
// Or: LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
Log.d("LAYOUT", mainLayout == null ? "NULL" : "NOT NULL"); // Always prints null
布局始终为空。
这是为什么呢? id是正确的,其他元素都可以毫无问题的获取。
【问题讨论】:
-
必须先膨胀/创建视图,以便 findViewById() 可以访问它。这个视图是可见的(例如显示在一个活动上)还是你需要在这个上下文之外获取它?
-
不确定,但我认为你必须尝试干净构建你的项目。