android ui标识符的奇怪行为

时间:2011-12-02 17:17:34

标签: android xml-layout

  

可能重复:
  Nullpointerexception when findingViewById

嗨,请有人解释一下这个吗? 我有这个XML

<com.simekadam.blindassistant.ui.BlindTableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/blindTableLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="0px"
    android:layout_margin="0px"
    android:focusable="true"
    android:stretchColumns="*"

    android:background="@android:color/white"
    >
    <TableRow android:id="@+id/tableRow1" android:layout_weight="1">
        <com.simekadam.blindassistant.ui.BlindButton android:id="@+id/first"  android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="0.5"/>
        <com.simekadam.blindassistant.ui.BlindButton android:id="@+id/secondButton" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="0.5"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <com.simekadam.blindassistant.ui.BlindButton android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"/>
        <com.simekadam.blindassistant.ui.BlindButton android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"/>
    </TableRow>
    <TableRow android:layout_weight="1">
        <com.simekadam.blindassistant.ui.BlindButton android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"/>
        <com.simekadam.blindassistant.ui.BlindButton android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"/>
    </TableRow>

</com.simekadam.blindassistant.ui.BlindTableLayout>

此代码在我试图设置Button的颜色的行上失败,并且出现NullPointerException。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.blindassistantmain);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    BlindTableLayout tableLayout = (BlindTableLayout) findViewById(R.id.blindTableLayout);
    TableRow row1 = (TableRow) tableLayout.findViewById(R.id.tableRow1);
    BlindButton btn1 = (BlindButton) row1.findViewById(R.id.first);
    btn1.setBackgroundColor(Color.RED);
 }

但是这段代码运行没有任何问题

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.blindassistantmain);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    BlindTableLayout tableLayout = (BlindTableLayout) findViewById(R.id.blindTableLayout);
    TableRow row1 = (TableRow) tableLayout.findViewById(R.id.tableRow1);
    BlindButton btn1 = (BlindButton) row1.getChildAt(0);
    btn1.setBackgroundColor(Color.RED);
 }

请问第一段代码到底有什么问题..? 在R.java中,我看到了我的身份证明......

0 个答案:

没有答案