为什么findViewById()的返回null,如果的setContentView()是不是叫什么名字? [英] Why findViewById() is returning null if setcontentview() is not called?

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

问题描述

我是新蜂到Android。

下面是我的xml文件 -

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=。MainActivity
    机器人:ID =@ + ID / LinearLayout中
    机器人:方向=垂直>

    <的TextView
     机器人:layout_width =WRAP_CONTENT
     机器人:layout_height =WRAP_CONTENT
     机器人:文本=@字符串/参考hello world
     机器人:ID =@ + ID / TextView的/>

  < / LinearLayout中>
 

和非常基本的code -

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    查看查看= getLayoutInflater()膨胀(R.layout.activity_main,空)。

            //的setContentView(视图);

            的LinearLayout LY =(的LinearLayout)findViewById(R.id.linearlayout);

            Log.i(System.out的,线性布局=+视图);

            Log.i(System.out的,线性布局=+ LY);
    }
 

输出:

  11月5日至10号:44:15.996:我/的System.out(6494):线性布局= android.widget.LinearLayout@41e34db8
11月5日至10日:44:15.996:我/的System.out(6494):线性布局= NULL
 

findViewById()被返回null?为什么呢?

如果我取消的setContentView(视图),然后再次运行。

输出:

  11月5日至10号:50:12.781:我/的System.out(7791):线性布局= android.widget.LinearLayout@41e0d6c8
11月5日至10日:50:12.781:我/的System.out(7791):线性布局= android.widget.LinearLayout@41e0d6c8
 

什么额外的做的setContentView()是干什么的?

解决方案

 公共无效的setContentView(查看视图)
 

设置活动内容,以明确的观点。这种观点被直接放入活动的视图层次结构。

的setContentView(查看视图)是活动类的方法。在创建活动中,你需要将内容设置为您的活动。

的onCreate(包)是你初始化你的活动。最重要的是,在这里你会通常所说的setContentView(视图)与布局资源定义你的用户界面,并使用findViewById(int)以获取你需要用编程方式进行交互在UI中的小部件。

您执行的onCreate(中)应该定义用户界面,并可能实例化一些类范围内的变量。

像文本视图每个资源,当在布局文件添加将在R.java中的条目可绘制文件中的条目是自动

示例

有关activity_main在R.java

 公共静态最后的客舱布局{
        公共静态最终诠释activity_main = 0x7f030000;
        }
 

在你的情况你膨胀的布局,但没有设置内容的活动。

您需要设置的内容到你的活动,然后找到使用findViewById(..)的标识。

如果不是你会得到NullPointerException异常。

I am new-bee to android.

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>

and Very basic code -

@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);
    }

Output:

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() is returning null? Why?

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

Output:

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

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 (View view) is a method of activity class. When activity is created you need to set the content to your activity.

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(view) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

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

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

Example

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.

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

If not you will get NullPointerException.

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

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