我是安卓的新手。
这是我的 xml 文件 -
<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"
tools:context=".MainActivity"
android:id="@+id/linearlayout"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/textview" />
</LinearLayout>
和非常基本的代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view=getLayoutInflater().inflate(R.layout.activity_main,null);
//setContentView(view);
LinearLayout ly = (LinearLayout)findViewById(R.id.linearlayout);
Log.i("System.out ","linear layout = " + view);
Log.i("System.out ","linear layout = " + ly);
}
输出:
05-10 11:44:15.996: I/System.out(6494): linear layout = android.widget.LinearLayout@41e34db8
05-10 11:44:15.996: I/System.out(6494): linear layout = null
findViewById()
返回null?为什么?
如果我取消注释setContentView(view)
并再次运行..
输出:
05-10 11:50:12.781: I/System.out(7791): linear layout = android.widget.LinearLayout@41e0d6c8
05-10 11:50:12.781: I/System.out(7791): linear layout = android.widget.LinearLayout@41e0d6c8
额外setContentView()
做什么?