findViewById返回null - 为什么?

3

我创建了一个自定义视图Game4,它继承自LinearLayout。当我试图找到它们时,它返回null(然后是nullPointerException,最后崩溃/!) 这是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);
}

游戏_4_布局.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>


7
logcat是一个Android系统上的命令行工具,用于查看和记录设备系统日志。 - nobalG
1
Game_4 tempGame = (Game_4) findViewById(R.id.game4);游戏_4 tempGame = (游戏_4) findViewById(R.id.game4); - Phantômaxx
我已经调试过了,它返回了null,并且tempGame在OnCreate函数之前被声明。 - mordicool
请发布您的Game_4代码,特别是构造函数部分。 - laalto
2个回答

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

你需要将 attrs 传递给 super 构造函数,例如:
super(context, attrs);

否则,由超类使用的属性,如android:id将会丢失。
您可以在这个两个参数的构造函数中进行自定义初始化,并从一个参数的构造函数中调用它。
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);
}

谢谢!非常有帮助。 - mordicool

-1

Game_4未被正确导入。在布局中,按下CTRL键并将光标移动到com.example.sortthegraph.Game_4,然后单击以转到相应的类。如果它没有直接转到相应的类,则可能存在一些问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接