开发者

Android dynamically change checkbox problem

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜