2011-12-01 72 views
0

我在onCreate方法的最後一行NullPointerException異常NullPointerException異常時findingViewById

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); 
    BlindButton btn = (BlindButton) findViewById(R.id.firstButton); 
    btn.setText("Lorem Ipsum"); //here is the nullpointer 
} 

我的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:layout_weight="1"> 
     <com.simekadam.blindassistant.ui.BlindButton android:id="@+id/firstButton" 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> 

有誰知道什麼是錯在我的代碼? 謝謝

+0

嘗試清理並重建項目。 – Petar

+0

我猜測它是因爲你的按鈕位於你的自定義組件內的一個表內。您可以通過tableLayout對象上的某個調用來訪問它。 –

回答

0

您是否嘗試清理您的項目? (「項目」 - >「清潔」) 這有助於我解決像你這樣的問題。 並且不要忘記檢查「自動構建」。

Btw。你可以在R.java中找到你的按鈕嗎?

+0

我不知道這是什麼意思,bu日食代碼提示提供給我 – simekadam

+0

所以它存在...並且它仍然不起作用 – simekadam

1

嘗試:

BlindTableLayout tableLayout = (BlindTableLayout) findViewById(R.id.blindTableLayout); 
BlindButton btn = (BlindButton) tableLayout.findViewById(R.id.firstButton); 

這樣,你發現你的tableLayout內來看,e.g tableLayout.findViewById(R.id.firstButton)。

+0

我試過這種方式實際上甚至beforr我張貼我的問題..但錯誤是還在那兒 – simekadam