2011-09-05 51 views
0

我有另一個問題。我在佈局中定義了CheckBoxes,並且在類中使用isChecked()方法獲取狀態。但是我得到每當這個方法被調用時,下面的錯誤日誌中的錯誤:Android複選框isChecked()拋出nullpointereception

09-05 14:55:58.457: ERROR/AndroidRuntime(582): FATAL EXCEPTION: main 
09-05 14:55:58.457: ERROR/AndroidRuntime(582): java.lang.IllegalStateException: Could not execute method of the activity 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.view.View$1.onClick(View.java:2144) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.view.View.performClick(View.java:2485) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.view.View$PerformClick.run(View.java:9080) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.os.Handler.handleCallback(Handler.java:587) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.os.Handler.dispatchMessage(Handler.java:92) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.os.Looper.loop(Looper.java:123) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at java.lang.reflect.Method.invoke(Method.java:507) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at dalvik.system.NativeStart.main(Native Method) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582): Caused by: java.lang.reflect.InvocationTargetException 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at java.lang.reflect.Method.invoke(Method.java:507) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at android.view.View$1.onClick(View.java:2139) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  ... 11 more 
09-05 14:55:58.457: ERROR/AndroidRuntime(582): Caused by: java.lang.NullPointerException 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  at alesito.WorkingHoursLogger.zapolniDelavnike.shraniUrnik(zapolniDelavnike.java:88) 
09-05 14:55:58.457: ERROR/AndroidRuntime(582):  ... 14 more 

這是類代碼:

private DatabaseAdapter dbOperator; 
private Cursor cursor; 
private static final String TABLE_NAME = "hours"; 
public static final String COL_ROWID = "_id"; 
public static final String COL_DATE = "date"; 
public static final String COL_DATE_TYPE = "dateType"; 
public static final String COL_DEF_HOURS = "defaultHours"; 
public static final String COL_COR_HOURS = "correctionHours"; 
public static final String COL_OPOMBA = "opomba"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dnevi); 
    dbOperator = new DatabaseAdapter(this); 
    dbOperator.open(); 

} 
public void zapolniDni(View view){ 
    setContentView(R.layout.zapolni); 
} 
public void shraniUrnik(View view) throws ParseException{ 
    DatePicker zacetniDatumPicker = (DatePicker)findViewById(R.id.zacetniDatum); 
    DatePicker koncniDatumPicker = (DatePicker)findViewById(R.id.koncniDatum); 


    TimePicker zacetniUraPicker = (TimePicker)findViewById(R.id.zacetnaUra); 
    Time zacetnaUra = new Time(); 
    zacetnaUra.set(0, zacetniUraPicker.getCurrentMinute(), zacetniUraPicker.getCurrentHour(), 0, 0, 0); 

    TimePicker koncniUraPicker = (TimePicker)findViewById(R.id.koncnaUra); 
    Time koncnaUra = new Time(); 
    koncnaUra.set(0, koncniUraPicker.getCurrentMinute(), koncniUraPicker.getCurrentHour(), 0, 0, 0); 

    double razlika = 0; 
    if (Time.compare(zacetnaUra, koncnaUra) < 0){ 
     double zacetniCas = zacetnaUra.hour * 60 + zacetnaUra.minute; 
     double koncniCas = koncnaUra.hour * 60 + koncnaUra.minute; 
     razlika = (koncniCas - zacetniCas)/60; 
    } 

    CheckBox ponedeljek = (CheckBox)findViewById(R.id.ponedeljek); 
    CheckBox torek = (CheckBox)findViewById(R.id.torek); 
    CheckBox sreda = (CheckBox)findViewById(R.id.sreda); 
    CheckBox cetrtek = (CheckBox)findViewById(R.id.cetrtek); 
    CheckBox petek = (CheckBox)findViewById(R.id.petek); 
    CheckBox sobota = (CheckBox)findViewById(R.id.sobota); 
    CheckBox nedelja = (CheckBox)findViewById(R.id.nedelja); 


    GregorianCalendar gcal = new GregorianCalendar(); 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.d"); 
    Date startDate = sdf.parse(zacetniDatumPicker.getYear()+"."+ (zacetniDatumPicker.getMonth()+1)+"."+ zacetniDatumPicker.getDayOfMonth()); 
    Date endDate = sdf.parse(koncniDatumPicker.getYear()+"."+ (koncniDatumPicker.getMonth()+1)+"."+ (koncniDatumPicker.getDayOfMonth()+1)); 


    gcal.setTime(startDate); 
    while (gcal.getTime().before(endDate)){ 
     switch (gcal.getTime().getDay()){ 
      case 0: 
       if (nedelja.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 1: 
       if (ponedeljek.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 2: 
       if (torek.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 3: 
       if (sreda.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 4: 
       if (cetrtek.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 5: 
       if (petek.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      case 6: 
       if (sobota.isChecked()){ 
        dbOperator.createDay(Integer.toString(gcal.getTime().getYear())+Integer.toString((gcal.getTime().getMonth()+1))+Integer.toString(gcal.getTime().getDate()), gcal.getTime().toString(), razlika, 0.0, ""); 
       } 
      default: 
       Toast nicOznaceno = Toast.makeText(this, "nič ni označeno", Toast.LENGTH_LONG); 
       nicOznaceno.show(); 
     } 
     gcal.add(gcal.DAY_OF_YEAR, 1); 
    } 
} 

這是佈局文件:

xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbars=""> 
    <TextView 
     android:id="@+id/navodiloDnevi" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:text="@string/navodiloDnevi"/> 
    <CheckBox 
     android:id="@+id/ponedeljek" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/navodiloDnevi" 
     android:text="@string/ponedeljek"/> 
    <CheckBox 
     android:id="@+id/torek" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/ponedeljek" 
     android:text="@string/torek"/> 
    <CheckBox 
     android:id="@+id/sreda" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/torek" 
     android:text="@string/sreda"/> 
    <CheckBox 
     android:id="@+id/cetrtek" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/sreda" 
     android:text="@string/cetrtek"/> 
    <CheckBox 
     android:id="@+id/petek" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/cetrtek" 
     android:text="@string/petek"/> 
    <CheckBox 
     android:id="@+id/sobota" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/petek" 
     android:text="@string/sobota"/> 
    <CheckBox 
     android:id="@+id/nedelja" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/sobota" 
     android:text="@string/nedelja"/> 
    <Button 
     android:id="@+id/posljiIzbraneDni" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/posljiIzbraneDni" 
     android:onClick="zapolniDni" 
     android:layout_below="@id/nedelja" 
     android:layout_centerHorizontal="true"/> 
</RelativeLayout> 

爲什麼我會收到錯誤?

編輯:

好吧,我會進一步指定。那裏有源代碼的類調用了兩個佈局。被稱爲(R.layout.dnevi)的第一個佈局包含複選框(附加的xml源代碼)。當檢查完成並單擊按鈕時,將執行該類中的下一個方法,該方法顯示新佈局(R.layout.zapolni)。這個不包含任何複選框,但它使用第一個佈局(R.layout.dnevi)中的數據(選中/取消選中)。希望這可以幫助。

+0

請稍微具體一點 – Shlublu

回答

0

嗯,我找到了解決方案,因爲我在網上找到的東西都沒有幫助。 我爲這兩個佈局創建了單獨的活動,並且我用putExtra()把InChecked狀態放在了Intent中。

1

這不是CheckBox#isChecked()方法拋出NullPointerException,這是你的shraniUrnik(View)方法是。因此,我會斷定您的複選框沒有被findViewById()方法正確找到。這可能是由於選擇了錯誤的佈局造成的。我可以看到你從onCreatezapolniDni都撥打setContentView()。如果其中一個佈局不包含那些shraniUrnik(View)被應用後調用的複選框,則會拋出NullPointerException

相關問題