嗨我有一个checbox并且想要做一些事情如果勾选复选框但是当我检查是否用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" />
答案 0 :(得分:0)
尝试将复选框设置为某个值,即true或false,然后再将其复选为if。 把它放在你的xml里面android:checked =“true”。
答案 1 :(得分:0)
你错过了
</TableRow>
布局文件中的
尝试
chkChickenBreast.setChecked="true"
答案 2 :(得分:0)
可能是你的问题不在CheckBox中
线性布局ID
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);
您正在使用单独的xml文件,然后使用Inflater概念
试试这段代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Double priceChickenBreast = 30.00;
LinearLayout receiptLayout=(LinearLayout) getLayoutInflater().inflate(R.layout.receipt, null);
TextView menuChickenBreast = new TextView(this);
CheckBox chkChickenBreast = (CheckBox) findViewById(R.id.checkBox1);
if (chkChickenBreast.isChecked()) {
menuChickenBreast.setText("Chicken Breast 30.00");
receiptLayout.addView(menuChickenBreast);
}
}
答案 3 :(得分:0)
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);
我在你的xml中找不到R.id.receiptMainLinearLayout
。
所以null pointer exception
发生了
当你致电receiptLayout.addView(menuChickenBreast);