android - findViewById 返回 null - 为什么?

标签 android android-linearlayout findviewbyid

我创建了一个扩展 LinearLayout 的自定义 View Game4。当我尝试找到它们时,它会返回 null(然后是 nullPointerException,然后是 crash/!!) 这是 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>

这是我的 OnCreate 函数:

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

这是 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>

最佳答案

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

您需要将 attrs 传递给 super 构造函数,例如

super(context, attrs);

否则父类使用的属性如 android:id 将丢失。

您可以在这个双参数构造函数中执行自定义初始化,也可以从 1-arg 构造函数中调用它:

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

关于android - findViewById 返回 null - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585617/

相关文章:

android - 如何在 Android 中为非市场应用程序更新应用程序而不是替换应用程序

android - 在android中以横向模式在viewpager中使图像适合屏幕宽度

android - 如何将图片向左移动,文字向右移动?

android - 在 Android 中以编程方式添加 LinearLayout 不起作用

java - findViewByID() 不起作用。我应该怎么办?

Android: Gingerbread 上的 YouTubePlayerFragment 出现 NoClassDefFoundError

android - 处理来自 Chrome 的共享和其他操作

android - 布局的边框

android - 何时使用菜单项 ID 调用 findViewById 以确保它不为空?

java - android getViewById() NullPointerException