我main.xml
看起来像这样:/res/layout/main.xml是否描述View或ViewGroup?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
由于根元素是LinearLayout
,延伸ViewGroup
,为什么main.xml
得到变成了View
而不是ViewGroup
?举例来说,在我的主要Activity
类,我试图让子视图数量的LinearLayout
包含这样的:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup vg = (ViewGroup) findViewById(R.layout.main);
Log.v("myTag", "num children: " + vg.getChildCount());
但是当我打电话vg.getChildCount()
它崩溃。
这样做的正确方法是什么?
你看看LogCat的异常吗? Android开发者已经做了很好的工作,让你知道他们发布的Exceptions出了什么问题。 – 2010-07-27 03:25:45