FindViewById returns null - why?

I've created a custom Game4 view that extends LinearLayout. When I tried to find 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 ??

      

These are the constructors for Game_4:

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

      

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>

      

+3


source to share


2 answers


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

      

You need to pass constructor attrs

to constructor super

for example.

super(context, attrs);

      



Otherwise, the attributes used by the superclass are lost, eg android:id

.

You can create your own init in this 2-argument constructor and call it from the 1-arg constructor:

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

      

+1


source


Game_4 was not imported properly. Also in layout, press CTRL and move the cursor to com.example.sortthegraph.Game_4 and click to navigate to the corresponding class. If it is not in the appropriate class, a problem may arise.



-1


source







All Articles