Nullpointerexception при обнаружении ViewById - PullRequest
0 голосов
/ 01 декабря 2011

У меня есть nullpointerexception в последней строке моего метода oncreate

 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>

Кто-нибудь знает, что не так в моем коде?Спасибо

Ответы [ 2 ]

1 голос
/ 01 декабря 2011

Попробуйте:

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

Таким образом, вы находите представление в вашем tableLayout, например, tableLayout.findViewById (R.id.firstButton).

0 голосов
/ 01 декабря 2011

Вы пытались очистить свой проект? («Проект» -> «Чистый») Это помогает мне иногда с такими проблемами, как ваша. И не забудьте проверить «Автоматически строить».

Btw. Можете ли вы найти свою кнопку в R.java?

...