简体   繁体   English

让孩子从线性布局

[英]Getting Child from linear layout

I created layout file, consist of one linearlayout and two nested linearlayout inside the main one. 我创建了布局文件,该文件由一个线性布局和两个嵌套的线性布局组成。 When I using getParent method, it select the second nested linearlayout. 当我使用getParent方法时,它选择第二个嵌套的linearlayout。 My target was the first one nested linearlayout. 我的目标是第一个嵌套的linearlayout。 So I gives the first nested linearlayout a ID called linear_top. 因此,我给第一个嵌套的linearlayout分配了一个名为linear_top的ID。 Then I declared in the onCreateView, test and debug, I have no luck it shows that is null. 然后我在onCreateView中声明,测试和调试,但我没有运气表明它为null。

My target was AnimatedGifImageView 我的目标是AnimatedGifImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linear_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

  <TextView
    android:id="@+id/music_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Love Story"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <com.music.flow.lib.AnimatedGifImageView
      android:id="@+id/music_anim"
      android:layout_width="match_parent"
      android:layout_height="76dp"
      android:scaleType="fitXY"
      android:contentDescription="Animation"
       />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RatingBar
        android:id="@+id/music_rating"            
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:numStars="4"
        android:rating="3.5" />

    <ImageView
        android:id="@+id/btn_playmusic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:src="@drawable/resume" />
</LinearLayout>

OnCreateView OnCreateView

linearTop = (LinearLayout) v.findViewById(R.id.linear_top);                 
AnimatedGifImageView animatedGifImageView = 
                    (AnimatedGifImageView) linearTop.getChildAt(1); /* Null Exception */

Child views are indexed from 0. In other words, the first child view is 0, the second child view is 1... and so on. 子视图从0开始索引。换句话说,第一个子视图为0,第二个子视图为1 ...,依此类推。

Also, why don't you just get the the ImageView by it's ID? 另外,为什么不通过ID来获取ImageView?

AnimatedGifImageView musicAnim = (AnimatedGifImageView) findViewById(R.id.music_anim);
@Override
    public View onCreateView(View v, String name, Context context,
            AttributeSet attrs) {
        LinearLayout linearTop = (LinearLayout) v.findViewById(R.id.linear_top);
        ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1); // NullPointer

        return super.onCreateView(v, name, context, attrs);
    }

Does your code look like this? 您的代码看起来像这样吗? You are getting a NullPointerException because in onCreateView, the parameter View v may be null, as mentioned in the docs here . 你得到一个NullPointerException,因为onCreateView,参数View v可以为空,在该文档中提到这里 Are you using fragments? 您正在使用片段吗? If not, then don't use onCreateView . 如果不是,则不要使用onCreateView Place your code instead in onCreate like this: 像这样将代码放在onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout linearTop = (LinearLayout) findViewById(R.id.linear_top);
    ImageView animatedGifImageView = (ImageView) linearTop.getChildAt(1);

    setContentView(R.layout.activity_nested_linearlayout);
}

also, as what Sound Conception mentioned, you could just try to get the view directly by using its id instead of manually getting it using an index. 同样,正如Sound Conception所提到的,您可以尝试通过使用其ID直接获取视图,而不是使用索引手动获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM