Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么从充气()返回的根视图与从findViewById()返回的视图不匹配?_Android_Layout Inflater_Inflate_Android Inflate_Findviewbyid - Fatal编程技术网

Android 为什么从充气()返回的根视图与从findViewById()返回的视图不匹配?

Android 为什么从充气()返回的根视图与从findViewById()返回的视图不匹配?,android,layout-inflater,inflate,android-inflate,findviewbyid,Android,Layout Inflater,Inflate,Android Inflate,Findviewbyid,作为一个新手,我在理解充气()的一些基础知识方面又遇到了麻烦 这是我的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"

作为一个新手,我在理解充气()的一些基础知识方面又遇到了麻烦

这是我的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>
以及输出:

05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e28848

05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e29740

05-10 14:30:33.446: I/System.out(26806): linear layout = android.widget.LinearLayout@41e28848

我从输出的第1行和第3行了解到,一旦调用
setContentView()
它就会膨胀,因此调用此方法后,视图对象将在内存中。因此,在调用
findViewById()
时,它会在代码块中同时返回相同的linearlayout视图对象。(ly isequalto ly1)

但是,为什么输出的第2行中LinearLayout对象的地址不同,
linear layout=android.widget。LinearLayout@41e29740

负责此操作的代码是-

View view=getLayoutInflater().inflate(R.layout.activity_main,null);
我认为这将返回根视图,在本例中是LinearLayout

如果
R.layout.activity\u main
已经膨胀,并且布局没有变化(没有添加或删除任何视图/视图组),那么为什么对象(视图和ly1)的地址不匹配? 我试过这个-

View view=getLayoutInflater().inflate(R.layout.activity_main,null);
setContentView(view); 
LinearLayout ly1 = (LinearLayout)findViewById(R.id.linearlayout); 
Log.i("System.out ","linear layout = " + view); 
Log.i("System.out ","linear layout = " + ly1);
还有这个-

I/System.out(2603): linear layout = android.widget.LinearLayout@41e09e10

I/System.out(2603): linear layout = android.widget.LinearLayout@41e09e10
为什么在这种情况下ly1和view对象代表相同的地址?

inflate()
将始终返回一个新的
view
对象
ly
ly1
是同一个对象。为什么你的期望不同

从充气单据

从指定的xml资源展开新的视图层次结构


你的测试程序似乎无效。因为您没有正确比较对象。也许您的
ly1
应该按如下方式检索:

LinearLayout ly1 = (LinearLayout)view.findViewById(R.id.linearlayout);
我相信它应该返回与
视图
相同的日志输出


无论如何,膨胀视图将返回一个新对象,它不可能等于在setContentView

下定义的视图。我认为第二行返回的是视图内存方向,而不是它本身的布局

它们是不同的东西

我认为你误解了事情。有一个层次结构,遵循这个通用方案:“视图->视图组->布局”

一个是指视图(它是活动父级),其中包含ViewGroup->LayoutView,另一个是指LayoutView,它是View->ViewGroup的子级

所以,它们是不同的东西,分开存放,思想观点会指向布局观点

这就是对第二行的解释


第1行和第3行显示相同的方向,因为定义为相同的对象。

因为您设置了ContentView(R.layout.activity\u main);以及引用同一视图层次中的(R.id.linearlayout)ly和ly1初始化为同一linearlayout。感谢@laalto的澄清

     View view=getLayoutInflater().inflate(R.layout.activity_main,null);
公共视图膨胀(int资源,视图组根)

从指定的xml资源展开新的视图层次结构。如果出现错误,则抛出InflateException

参数

   resource ID for an XML layout resource to load (e.g., R.layout.main_page)

   root Optional view to be the parent of the generated hierarchy.
返回

膨胀层次结构的根视图。如果提供了root,则这是root视图;否则它就是膨胀的XML文件的根


因此getLayoutFlater()充气(R.layout.activity_main,null);返回一个新的视图对象

这是我测试的-

            View view=getLayoutInflater().inflate(R.layout.activity_main,null);
    Log.i("System.out ","view = " + view + "id = "+ view.getId());
    setContentView(view);
    ly = (LinearLayout)findViewById(R.id.linearlayout);
    Log.i("System.out ","linear layout = " + ly + "id="+ly.getId());


    View view1=getLayoutInflater().inflate(R.layout.activity_main,null);
    Log.i("System.out ","view1 = " + view1 + "id = "+ view1.getId());
    setContentView(view1);
    ly = (LinearLayout)findViewById(R.id.linearlayout);
    Log.i("System.out ","linear layout = " + ly + "id="+ly.getId());
在这两种情况下,ly对象具有不同的地址-

05-10 19:00:36.486: I/System.out(27233): view = android.widget.LinearLayout@41e0ef70id = 2131230720
05-10 19:00:36.731: I/System.out(27233): linear layout = android.widget.LinearLayout@41e0ef70id=2131230720

因此,上述输出将得出以下结论:-

[1.]inflate()方法将始终返回表示xml层次结构的新根视图对象

[2.]调用setContentView(view)时,“view”将与活动关联。

现在,基于这些关系,让我们理解这个线程中问题的输出

调用时-setContentView(R.layout.activity\u main)

安卓系统将解决通货膨胀问题。这意味着它将为xml中的所有元素创建视图对象。然后这些视图显示在屏幕上。因此,从这个角度来看,内存中的对象是LinearLayout@41e28848。由于这表示根视图(线性布局),因此调用-

LinearLayout ly=(LinearLayout)findViewById(R.id.LinearLayout)

ly也将指向相同的观点LinearLayout@41e28848,它是在调用setContentView()时创建的。所以,ly=LinearLayout@41e28848

这证明了第一条输出线

继续查看视图=GetLayoutFlater()。充气(R.layout.activity\u main,空)
, 这将再次创建具有不同地址位置的新视图(LinearLayout@41e29740)。这是xml的手动膨胀。但是这个视图对象与当前活动没有关联,因为我们没有调用setContentView(视图)。这里是视图=LinearLayout@41e29740

下一行-LinearLayout ly1=(LinearLayout)findViewById(R.id.LinearLayout), 再次获取当前活动的活动视图。这是(LinearLayout@41e28848)这也是根视图。这实际上是在setContentView()方法中创建的。因此,ly1=LinearLayout@41e28848

接下来的两个print语句只打印它们表示的对象,即view和ly1


谢谢。

视图膨胀(int资源,视图组根)
将返回膨胀层次结构的根视图。如果提供了root,则这是root视图;否则它是膨胀的XML文件的根。

您可以更改
LinearLayout ly1=(LinearLayout)findViewById(R.id.LinearLayout)
to
LinearLayout ly1=(LinearLayout)视图,并查看发生了什么。Flat()不能返回相同的对象,因为它应该创建一个新的视图层次结构。例如,它可以用于动态创建视图片段,就像在列表适配器中一样,在列表适配器中,您有不同的数据,但希望填充相同类型的视图(列表项)通过@comeGetSome检查注释。视图=GetLayoutFlater()。充气(R.layout.activity_main,
05-10 19:00:36.486: I/System.out(27233): view = android.widget.LinearLayout@41e0ef70id = 2131230720
05-10 19:00:36.731: I/System.out(27233): linear layout = android.widget.LinearLayout@41e0ef70id=2131230720
05-10 19:00:36.731: I/System.out(27233): view1 = android.widget.LinearLayout@41e2a5d0id = 2131230720
05-10 19:00:36.736: I/System.out(27233): linear layout = android.widget.LinearLayout@41e2a5d0id=2131230720