0

嗨,我有一个复选框,如果复选框被选中,我想做一些事情,但是当我检查它是否使用 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" />

4

4 回答 4

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

    }
于 2013-08-22T09:02:54.033 回答
0

尝试将复选框设置为某个值,即 true 或 false,然后再将其签入 if。把它放在你的 xml 里面 android:checked="true"。

于 2013-08-22T08:48:10.260 回答
0

你错过了

</TableRow>

在布局文件中

尝试

chkChickenBreast.setChecked="true"
于 2013-08-22T08:49:50.913 回答
0
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout);

R.id.receiptMainLinearLayout我在你的 xml 中找不到。所以null pointer exception当你打电话时发生 了receiptLayout.addView(menuChickenBreast);

于 2013-08-22T09:07:39.897 回答