2011-05-07 11 views
0

こんにちは 私は自分のチェックボックスを動的に変更しようとしています。何らかの理由で アプリが私の例外をスローします。 何が間違っていますか? これは私のJavaコードです:アンドロイドは動的にチェックボックスの問題を変更します

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); 
} 

} 

と、これは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> 

私はその簡単な質問と確信しているが、私はどこにでも答えを見つけることができません。 p.s. 後でユーザ設定からチェックボックスを変更したいので、xmlでチェックボックスの値をtrueに設定することはできません。 10倍!

答えて

1

通常、このような質問をするときに例外メッセージを投稿する必要がありますが、あなたの問題は、setContentViewの前にfindViewByIdにしようとしているようです。探しているときにあなたのビューが存在することはありません。

+0

私はそれが簡単な質問だったことを知っていた:) – doron

関連する問題