AndroidBeginner
AndroidBeginner

Reputation: 153

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. My target was the first one nested linearlayout. So I gives the first nested linearlayout a ID called linear_top. Then I declared in the onCreateView, test and debug, I have no luck it shows that is null.

My target was 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

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

Upvotes: 0

Views: 2070

Answers (2)

chjarder
chjarder

Reputation: 613

@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. Are you using fragments? If not, then don't use onCreateView. Place your code instead in onCreate like this:

@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.

Upvotes: 0

Sound Conception
Sound Conception

Reputation: 5411

Child views are indexed from 0. In other words, the first child view is 0, the second child view is 1... and so on.

Also, why don't you just get the the ImageView by it's ID?

AnimatedGifImageView musicAnim = (AnimatedGifImageView) findViewById(R.id.music_anim);

Upvotes: 1

Related Questions