2013-08-22 52 views
0

您好我有一個checbox,並希望如果複選框被選中做一些事情,但我得到一個NullPointerException異常錯誤,當我檢查,如果它與if語句檢查:Android的複選框返回顯示java.lang.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

你確定錯誤是在這條線,「如果(chkChickenBreast.isChecked()){」? –

+0

你在佈局文件末尾忘了一個'' – Sw4Tish

+0

檢查你的xml文件,你丟失 – Jilberta

回答

0

可能是你的問題不是在複選框

線性佈局ID

LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout); 

您正在使用單獨的XML文件,然後使用吹氣概念

試試這個代碼

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

    } 
+0

在另一個名爲order.xml的xml文件中。 – PaulPolon

+0

將id屬性添加到您的佈局android:id =「@ + id/receiptMainLinearLayout」 – Venky

+0

然後使用inflater概念 – Venky

0

嘗試將複選框設置爲某個值即真或假,如果你選擇了它之前。 把這個放在android裏的xml裏:checked =「true」。

+0

我試圖把'android:checked =「false」'但是它仍然給我的錯誤 – PaulPolon

+0

你可以嘗試把你的,如果這裏面chekbox.setOnCheckedChangeListener(新OnCheckedChangeListener(){ \t公共無效onCheckedChanged(CompoundButton爲arg0,ARG1布爾){ \t} }); – Shrikant

0

您在佈局文件中錯過

</TableRow> 

嘗試

chkChickenBreast.setChecked="true" 
+0

@ Sw4Tish thnaks ..但錯誤仍然存​​在 - PaulPolon 2分鐘前 – Sw4Tish

0
LinearLayout receiptLayout = (LinearLayout)findViewById(R.id.receiptMainLinearLayout); 

我不覺得在你的XML中R.id.receiptMainLinearLayout。 所以null pointer exception發生 當你調用receiptLayout.addView(menuChickenBreast);