嗨,我有一个复选框,如果复选框被选中,我想做一些事情,但是当我检查它是否使用 if 语句检查时,我得到一个 nullpointerexception 错误:
if (chkChickenBreast.isChecked()){
\\Do Something;
}
我的receiptActivity.java中有这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receipt);
Double priceChickenBreast = 30.00;
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);
TextView menuChickenBreast = new TextView(this);
final CheckBox chkChickenBreast = (CheckBox) findViewById(R.id.checkBox1);
try{
if (chkChickenBreast.isChecked()){
menuChickenBreast.setText("Chicken Breast Php 30.00");
receiptLayout.addView(menuChickenBreast);
}
}catch(Exception e){
menuChickenBreast.setText(e.toString());
receiptLayout.addView(menuChickenBreast);
}
}
}
我的 order.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99FF50"
android:gravity="top"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/scrollView_Main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow_Menu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView_MenuTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/order_title"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/menu_one"
android:checked="false"
android:textSize="12sp" />
<ImageView
android:id="@+id/imageView_Menu1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:contentDescription="@string/menu_one"
android:src="@drawable/chickenbreast" />
</TableRow>
</TableLayout>
</ScrollView>
和我的 Receipt.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/receiptMainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99FF50"
android:gravity="top"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textViewReceiptTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/receipt_title"
android:textAppearance="?android:attr/textAppearanceLarge" />