2011-09-05 3 views
0

J'ai un autre problème. J'ai défini CheckBoxes dans la mise en page et j'obtiens leur état avec la méthode isChecked() dans une classe. Cependant, je reçois une erreur chaque fois que cette méthode est appelée avec le journal d'erreur suivant:Android CheckBox isChecked() jette 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 

C'est le code de classe:

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

Et c'est un fichier de mise en page:

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> 

Pourquoi ai-je les erreurs?

EDIT:

Ok, je vais préciser davantage. La classe qui a la source là-haut appelle deux mises en page. La première mise en page appelée (R.layout.dnevi) contient les cases à cocher (source xml attachée). Lorsque la vérification est terminée et que le bouton est cliqué, la méthode suivante de la classe est exécutée et affiche une nouvelle disposition (R.layout.zapolni). Celui-ci ne contient pas de cases à cocher mais il utilise les données (cochées/décochées) de la première mise en page (R.layout.dnevi). J'espère que cela t'aides.

+0

Veuillez être un peu plus précis – Shlublu

Répondre

0

Eh bien, j'ai trouvé une solution, car rien de ce que j'ai trouvé sur le web n'a aidé. J'ai créé des activités séparées pour les deux mises en page et j'ai mis le statut isChecked dans Intent avec putExtra().

1

Ce n'est pas la méthode CheckBox#isChecked() qui lance le NullPointerException, c'est votre méthode shraniUrnik(View) qui est. Ainsi, je conclurais que vos cases à cocher ne sont pas correctement trouvées par la méthode findViewById(). Cela peut être dû à une mise en page incorrecte sélectionnée. Je vois que vous appelez setContentView() à la fois onCreate et zapolniDni. Si l'une de ces mises en page ne contient pas de cases à cocher avec les shraniUrnik(View) appelées après leur application, le NullPointerException sera lancé.