0

I'm trying to change my checkbox dynamically. for some reason the app throw my an exception. what am I doing wrong? this is my java code:

package com.school.commtest;

import android.app.Activity; 
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;

public class OtherPlacesMenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CheckBox c1 = (CheckBox)this.findViewById(R.id.schools);

    c1.setChecked(true);

    setContentView(R.layout.otherplacesmenu);
}

}

and this is the xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/table">
   <TableRow android:layout_height="fill_parent" android:layout_width="wrap_content"  android:id="@+id/tableRow1">
    <CheckBox android:text="School" android:id="@+id/schools" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="@+id/tableRow2">

    <CheckBox android:text="Supermaker" android:layout_height="wrap_content" android:id="@+id/supermarkets" android:layout_width="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="@+id/tableRow3">

    <CheckBox android:text="Gardens" android:layout_height="wrap_content" android:id="@+id/gardens" android:layout_width="wrap_content"></CheckBox>
 </TableRow>
</TableLayout>

I'm sure its a simple question, but I cannot find the answer anywhere. p.s. I can't set the value of the checkbox to be true at the xml because I would later want to change the checkbox from the user setting. 10x!

1 Answer 1

1

Usually you should post the exception message when asking a question like this, but it looks like your problem is that you're trying to findViewById before you setContentView. Your view doesn't exist to be found when you look for it.

Sign up to request clarification or add additional context in comments.

1 Comment

i knew it was an easy question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.