2011-12-01 56 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