2016-05-14 21 views
0

Der Code funktionierte gut, als ich 2 weitere Datensätze in die Datenbank aufgenommen habe und es begann, Probleme zu machen. Hier ist mein Code:Android Coursor außerhalb der Grenzen

//Workout Name Decider 
    public String Workout_Name(String id_S) { 
     String workout_name = ""; 
     String template = ""; 
     String holder = ""; 

     if (id_S.equals("REST")) { 
      workout_name = "Rest"; 
     } else { 
      String[] allColumns = new String[]{ 
        Dbhelper.WORKOUTS_ID, 
        Dbhelper.WORKOUT_NAME, 
        Dbhelper.WORKOUT_TIME 

      }; 
      Cursor cursor = database.query(Dbhelper.TABLE_WORKOUTS_STOCK, allColumns, "_id=\'" + id_S + "\'", null, null, null, null, "1"); 

      if (cursor != null) { 
       cursor.moveToFirst(); 
       //id = cursor.getString(2); 

       if (cursor.getString(1).contains("SPARTAN SPECIALE ADVANCED") || cursor.getString(1).contains("SPARTAN SPECIALE BEGINNER") || cursor.getString(1).contains("TRICEPS SMASHER REPS")) { 
        template = "(reps)"; 
       } else { 
        template = "(" + cursor.getString(2) + ")"; 
       } 

      } 


      try { 
       workout_name_shortener = cursor.getString(1).split("\\s+"); 
      } catch (PatternSyntaxException ex) { 
      } 

      int counter = 0; 

      for (int i = 0; i < workout_name_shortener.length; i++) { 

       if (workout_name_shortener[i].equals("TIMER") || workout_name_shortener[i].equals("REPS") || workout_name_shortener[i].equals("ADVANCED") || workout_name_shortener[i].equals("BEGINNER") && counter != 0) { 
        break; 
       } else { 
        holder += workout_name_shortener[i] + " "; 
       } 

       counter++; 
      } 

      workout_name = holder + template; 


      cursor.close(); 
     } 


     return workout_name; 
    } 

Und hier ist der Fehler:

FATAL EXCEPTION: main Process: com.spartanbodyweightworkouts, PID: 9198 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spartanbodyweightworkouts/com.spartanbodyweightworkouts.tabs.plan.planAdapter.planAdapter.PlanAdapter}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.....

Antwort

0

Ihre ändern if-Anweisung dazu:

if (cursor != null && cursor.moveToNext()) { 
    //id = cursor.getString(2); 
    if (cursor.getString(1).contains("SPARTAN SPECIALE ADVANCED") || cursor.getString(1).contains("SPARTAN SPECIALE BEGINNER") || cursor.getString(1).contains("TRICEPS SMASHER REPS")) { 
    template = "(reps)"; 
    } else { 
    template = "(" + cursor.getString(2) + ")"; 
    } 
} 
+1

Danke, aber der Code oben funktioniert, das Problem war in meiner Datenbank.Ich habe keinen Datensatz geschlossen und es wurde gemischt. –

0

Der Code funktioniert, ich einen Fehler gemacht, während Daten in die Tabelle einfügen.

Verwandte Themen