Nullpointerexception when findingViewById

試著忘記壹切 提交于 2020-01-11 13:44:09

问题


I have nullpointerexception in last line of my oncreate method

 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
 }

My XML looks like this

<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>

Does anybody know whats wrong in my code? Thank you


回答1:


Try:

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

This way you're finding the view within your tableLayout, e.g tableLayout.findViewById(R.id.firstButton).




回答2:


Did you try to clean your project? ("Project" -> "Clean") This helps me sometimes with problems like yours. And dont forget to check "Build Automaticly".

Btw. Can you find your button in the R.java ?



来源:https://stackoverflow.com/questions/8341393/nullpointerexception-when-findingviewbyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!