2014-10-27 104 views
3

我创建了一个扩展LinearLayout的自定义视图Game4。当我试图找到他们时,现场并返回null(然后NullPointerException异常,然后崩溃/ !!) 这是activity_play_game.xml:findViewById返回null - 为什么?

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

的克ame_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> 

+7

logcat的????????? – nobalG 2014-10-27 10:38:21

+1

Game_4 tempGame =(Game_4)findViewById(R.id.game4); – 2014-10-27 10:41:27

+0

我已经调试过它,并且它返回null,并且在OnCreate函数之前声明tempGame .. – mordicool 2014-10-27 11:12:35

回答

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

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

super(context, attrs); 

否则,诸如android:id之类的超类消耗的属性将丢失。

您可以在此两个参数的构造做您的自定义初始化,并从1参数的构造函数调用它还有:

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

谢谢!很有帮助。 – mordicool 2014-10-27 12:24:13

-1

Game_4没有正确导入。在布局中,按下CTRL并将光标移至com.example.sortthegraph.Game_4并单击以转到相应的类。如果它没有指向相应的班级,那么可能会有一些问题。