Search code examples
androidandroid-linearlayoutfindviewbyid

findViewById returns null - why?


I have created a custom view Game4 that extends LinearLayout. When I tried finding them, it field and returned null (and then nullPointerException and then crash/!!) This is activity_play_game.xml:

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sortthegraph.PlayGameActivity"
tools:ignore="MergeRootFrame" >

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

    <com.example.sortthegraph.Game_4 // A custom View extends LinearLayout
        android:id="@+id/game4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >
    </com.example.sortthegraph.Game_4>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="#000000" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

This is my OnCreate function:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_game);        
    tempGame = (Game_4) findViewById(R.id.game4); // ?? Why null ??

This is Game_4 constructors:

public class Game_4 extends LinearLayout {
public Game_4(Context context, AttributeSet attrs) {
    this(context);
}
public Game_4(Context context) {
    super(context);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.game_4_layout, this, true);
    this.setWillNotDraw(false);
}

The game_4_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="20dp"
    android:paddingBottom="25dp"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:gravity="center" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <View android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <com.example.sortthegraph.BackButton
        android:id="@+id/backButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp" >
    </com.example.sortthegraph.BackButton>

    <View android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="20dip"
        android:layout_marginTop="20dip" >

        <com.example.sortthegraph.BackButton
            android:id="@+id/backButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.example.sortthegraph.BackButton>

        <View 
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <com.example.sortthegraph.BackButton
            android:id="@+id/backButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </com.example.sortthegraph.BackButton>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <View android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

        <com.example.sortthegraph.BackButton
            android:id="@+id/backButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp" >
        </com.example.sortthegraph.BackButton>

        <View android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    </TableRow>


Solution

  • public Game_4(Context context, AttributeSet attrs) {
        this(context);
    }
    

    You need to pass the attrs to the super constructor, e.g.

    super(context, attrs);
    

    Otherwise the attributes consumed by the superclass such as android:id are lost.

    You can do your custom init in this two-arg constructor and call it from the 1-arg constructor as well:

    public Game_4(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.game_4_layout, this, true);
        this.setWillNotDraw(false);
    }
    
    public Game_4(Context context) {
        this(context, null);
    }