如果未调用 setcontentview(),为什么 findViewById() 返回 null? [英] Why findViewById() is returning null if setcontentview() is not called?

查看:33
本文介绍了如果未调用 setcontentview(),为什么 findViewById() 返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是安卓的新手.

这是我的 xml 文件 -

Here is my xml file -

<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?为什么?

findViewById() is returning null? Why?

如果我取消注释 setContentView(view) 并再次运行..

If I uncomment setContentView(view) and run again ..

输出:

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() 做了什么额外的事情?

What extra does setContentView() is doing?

推荐答案

   public void setContentView (View view)

将活动内容设置为显式视图.此视图直接放置在活动的视图层次结构中.

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

setContentView(视图视图)是activity类的一个方法.创建活动时,您需要将内容设置为您的活动.

setContentView (View view) is a method of activity class. When activity is created you need to set the content to your activity.

onCreate(Bundle) 是您初始化活动的地方.最重要的是,在这里您通常会使用定义 UI 的布局资源调用 setContentView(view),并使用 findViewById(int) 检索该 UI 中您需要以编程方式与之交互的小部件.

您的 onCreate() 实现应该定义用户界面并可能实例化一些类范围的变量.

Your implementation of onCreate() should define the user interface and possibly instantiate some class-scope variables.

每个资源,如文本视图、可绘制对象,当添加到布局文件中时,都会在 R.java 文件中拥有一个条目该条目是自动的

Every resource like text view ,drawables when added in layout files will have an entry in R.java files The entry is automatic

示例

对于 R.java 中的 activity_main

For activity_main in R.java

        public static final class layout {
        public static final int activity_main=0x7f030000;
        }

在您的情况下,您会夸大布局但未将内容设置为活动.

In your case your inflating the layout but not setting the content to the activity.

您需要将内容设置为您的活动,然后使用 findViewById(..) 查找 ID.

You need to set the content to your activity and then find the ids using findViewById(..).

否则,您将收到 NullPointerException.

If not you will get NullPointerException.

这篇关于如果未调用 setcontentview(),为什么 findViewById() 返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆