2016-05-21 6 views
0

初めてアプリを読み込んだときに、データがアプリに追加され、ユーザーは自分が望むものを使用できます。 問題は、同じテーブルにさらにデータを追加したときに、更新でさらにデータを追加するということです。私は私のカーソルがnullであることを考え出した0データベースにデータを追加した後にカーソルがnullになる

のサイズとインデックス0が要求され、 :

android.database.CursorIndexOutOfBoundsException:コードより がクラッシュすると、その私に教えて開始しますしかし、なぜ私は考えていない。

データベースが開かれていないため、正しく閉じました。ここで が私のコードです:

データベース部への追加:

public class Database { 


    private Dbhelper dbhelper; 
    SQLController dbcon; 
    private SQLiteDatabase database; 
    Context context; 

    public Database(Dbhelper dbhelper, SQLController dbcon, SQLiteDatabase database, Context context) { 
     this.dbhelper = dbhelper; 
     this.database = database; 
     this.dbcon = dbcon; 
     this.context = context; 
    } 


    public boolean addToDatabase() { 
     boolean isTrue = false; 
     /// 
     try { 

      dbcon.open(); 
      int plan_i = dbcon.countPlanStock().getCount(); 
      dbcon.close(); 

      if (plan_i < 3) { 

       dbcon.open(); 
       dbcon.deletePlanSAll(); 
       dbcon.close(); 

       //REST = REST 
       dbcon.open(); 
       dbcon.insertPlanStock("Newbie", "Free", "ic_newbie", "7 Days", "5 8 12 REST 7 REST 1", "false false false false false false false"); 
       dbcon.close(); 

       dbcon.open(); 
       dbcon.insertPlanStock("Alpha 10", "Pro version", "ic_alphaten", "14 Days", "1 8 5 12 4 10 REST 11 REST 1 6 REST 2 5", "false false false false false false false false false false false false false false"); 
       dbcon.close(); 

       dbcon.open(); 
       dbcon.insertPlanStock("Alpha V2", "Pro version", "ic_alphav2", "30 Days", "1 3 11 10 13 REST REST 5 4 11 12 8 REST REST 3 7 13 11 REST 13 12 REST REST 6 12 4 1 REST 12 13", "false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false"); 
       dbcon.close(); 
      } 


      dbcon.open(); 
      int workout_i = dbcon.countDataS().getCount(); 
      dbcon.close(); 

      if (workout_i < 17) { 

       dbcon.open(); 
       dbcon.deleteAllFromStockWorkouts(); 
       dbcon.close(); 

       dbcon.open(); 
       dbcon.insertDataStock("INTENSE MMA CIRCUIT", " ENDURANCE ", " DEFINITION ", " CORE ", "16min", "NO EQUIPMENT REQUIRED", "3", 
         "pushups uppercuts slow_pushups quick_punches",//round 1 
         "jump_squat_180 burpee_hill_climb short_jumps",//round 2 
         "leg_raises tyson_neck_raise spartan_pushups",//round 3 
         "",//round 4 
         "",//round 5 
         "",//round 6 
         "",//round 7 
         "",//round 8 
         "",//round 9 
         "",//round 10 
         "",//round 11 
         "",//round 12 


         "40000",//rest 1 
         "40000",//rest 2 
         "40000",//rest 3 
         "",//rest 4 
         "",//rest 5 
         "",//rest 6 
         "",//rest 7 
         "",//rest 8 
         "",//rest 9 
         "",//rest 10 
         "",//rest 11 
         "",//rest 12 

         "10x 30x 5x 50x",//reps round 1 
         "5x 10x 10x",//reps round 2 
         "10x 5x 10x",//reps round 3 
         "",//reps round 4 
         "",//reps round 5 
         "",//reps round 6 
         "",//reps round 7 
         "",//reps round 8 
         "",//reps round 9 
         "",//reps round 10 
         "",//reps round 11 
         "",//reps round 12 

         "intense_mma_circuit_1",//round exercises 1 
         "intense_mma_circuit_2",//round exercises 2 
         "intense_mma_circuit_3",//round exercises 3 
         "",//round exercises 4 
         "",//round exercises 5 
         "",//round exercises 6 
         "",//round exercises 7 
         "",//round exercises 8 
         "",//round exercises 9 
         "",//round exercises 10 
         "",//round exercises 11 
         "",//round exercises 12 

         "300000",//reps round 1 main 
         "300000",//reps round 2 main 
         "300000",//reps round 3 main 
         "",//reps round 4 main 
         "",//reps round 5 main 
         "",//reps round 6 main 
         "",//reps round 7 main 
         "",//reps round 8 main 
         "",//reps round 9 main 
         "",//reps round 10 main 
         "",//reps round 11 main 
         "",//reps round 12 main 


         "10",//rest exercise round 1 main 
         "10",//rest exercise round 2 main 
         "10",//rest exercise round 3 main 
         "",//rest exercise round 4 main 
         "",//rest exercise round 5 main 
         "",//rest exercise round 6 main 
         "",//rest exercise round 7 main 
         "",//rest exercise round 8 main 
         "",//rest exercise round 9 main 
         "",//rest exercise round 10 main 
         "",//rest exercise round 11 main 
         "",//rest exercise round 12 main 
         "0",//workout type 


         "- - -",//Exercise Rest Round 1 
         "- -",//Exercise Rest Round 2 
         "- -",//Exercise Rest Round 3 
         "",//Exercise Rest Round 4 
         "",//Exercise Rest Round 5 
         "",//Exercise Rest Round 6 
         "",//Exercise Rest Round 7 
         "",//Exercise Rest Round 8 
         "",//Exercise Rest Round 9 
         "",//Exercise Rest Round 10 
         "",//Exercise Rest Round 11 
         ""//Exercise Rest Round 12 


       ); 
       dbcon.close(); 


       ...... 
      } 



     } finally { 
      isTrue = true; 
     } 


     return isTrue; 
    } 


} 

ヘルパーとして、この、私はちょうどこのクラスの世話をする非同期タスクがあります。

データが後で追加されているときに問題が発生したアダプタクラス:

public class PlanAdapter extends AppCompatActivity { 
    private Toolbar toolbar; 
    private Dbhelper dbhelper; 
    SQLController dbcon; 
    private SQLiteDatabase database; 
    SQLController adapter; 
    String[] wokrouts_A; 
    String wokrouts_S; 

    public String[] isDone_A; 
    public String isDone_S; 
    public boolean isDone[]; 
    private ProgressBar progressBar; 
    public int bool_counter = 0; 

    private String Plan_Name = "TEST"; 
    private TextView Plan_Days_Coutner; 

    RecyclerView recyclerView; 
    PlanRecyclerAdapter ca; 

    int current_day = 0; 

    String[] workout_name_shortener; 


    private DBhelper dbhelper_l; 
    private SQLiteDatabase database_l; 
    SQLiteController dbcon_l; 
    String date = new SimpleDateFormat("d.M.yyyy").format(new Date()); 

    ProgressBar pm_navigation; 
    List<Integer> Tab_Colour = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.plan_adapter); 


     adapter = new SQLController(this); 
     dbcon = new SQLController(this); 
     dbhelper = new Dbhelper(this); 
     database = dbhelper.getWritableDatabase(); 


     dbcon_l = new SQLiteController(this); 
     dbhelper_l = new DBhelper(this); 
     database_l = dbhelper_l.getWritableDatabase(); 

     Intent intent = getIntent(); 
     Plan_Name = intent.getStringExtra("plan_info"); 

     toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     Typeface faceRobotoMedium = Typeface.createFromAsset(getResources().getAssets(), "Roboto-Medium.ttf"); 
     TextView TitleBarCustom = (TextView) findViewById(R.id.toolbar_title); 
     TitleBarCustom.setTypeface(faceRobotoMedium); 

     TitleBarCustom.setText("" + Plan_Name); 
     getSupportActionBar().setTitle(null); 

     Plan_Days_Coutner = (TextView) findViewById(R.id.plan_coutner_id); 
     Plan_Days_Coutner.setTypeface(faceRobotoMedium); 


     recyclerView = (RecyclerView) findViewById(R.id.recyclerList); 
     LinearLayoutManager llm = new LinearLayoutManager(this); 
     llm.setOrientation(LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(llm); 
     ca = new PlanRecyclerAdapter(generateWorkouts(), isDone, bool_counter, Plan_Name, getApplicationContext(), dbhelper, dbcon, database); 
     //ca.setClickListener(this); 
     recyclerView.setAdapter(ca); 

     progressBar = (ProgressBar) findViewById(R.id.progressBar); 
     progressBar.setProgress(current_day); 
     progressBar.setMax(isDone.length); 

     if (current_day == isDone.length) { 
      Plan_Days_Coutner.setText("" + isDone.length + " DAYS" + " PLAN COMPLETED"); 


     } else { 
      Plan_Days_Coutner.setText("DAY " + current_day + "/" + isDone.length); 
     } 


     for (boolean n : isDone) { 
      Log.d("Seud", "_" + n); 
     } 


    } 

    private ArrayList<PlanRecycler> generateWorkouts() { 
     ArrayList<PlanRecycler> palettes = new ArrayList<>(); 

     String[] allColumns = new String[]{Dbhelper.PLAN_ID, 
       Dbhelper.PLAN_NAME, 
       Dbhelper.PLAN_UNDER_TEXT, 
       Dbhelper.PLAN_DURATION, 
       Dbhelper.PLAN_IMAGE, 
       Dbhelper.PLAN_WOKROUTS_ID, 
       Dbhelper.PLAN_IS_FINISHED 
     }; 

     Cursor cursor_s = database.query(Dbhelper.TABLE_PLAN_STOCK, allColumns, "_name=\'" + Plan_Name + "\'", null, null, null, null, "1"); 

     if (cursor_s != null) { 
      cursor_s.moveToFirst(); 
      wokrouts_S = cursor_s.getString(5); 
      isDone_S = cursor_s.getString(6); 
     } 

     try { 
      wokrouts_A = wokrouts_S.split("\\s+"); 
      Log.d("Workouts_name", "" + wokrouts_A); 
     } catch (PatternSyntaxException ex) { 
     } 

     try { 
      isDone_A = isDone_S.split("\\s+"); 
     } catch (PatternSyntaxException ex) { 
     } 



     Boolean_Generator(isDone_A); 

     if (cursor_s.moveToFirst()) { 
      do { 

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

        palettes.add(new PlanRecycler(
          Workout_Name(wokrouts_A[i]), Workout_Name_Holder(wokrouts_A[i]), Tab_Colour.get(i) 

        )); 

       } 
      } while (cursor_s.moveToNext()); 
     } 
     cursor_s.close(); 

     return palettes; 
    } 

    //Workout Name Decider 
    public String Workout_Name_Holder(String id_S) { 
     String workout_name = ""; 
     if (id_S.equals("REST")) { 
      workout_name = "Rest"; 
     } else { 
      String[] allColumns = new String[]{ 
        Dbhelper.WORKOUTS_ID, 
        Dbhelper.WORKOUT_NAME, 

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


      if(cursor != null && cursor.moveToFirst()){ 
       workout_name = cursor.getString(1); 
       cursor.close(); 
      } 

      /* 
      if (cursor != null) { 
       cursor.moveToFirst(); 
       //id = cursor.getString(2); 
       workout_name = cursor.getString(1); 
      } 
      cursor.close(); 
      */ 
     } 
     return workout_name; 
    } 

    //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()){ 
       Log.d("Cursor_info","not_null"); 
       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) + ")"; 
       } 

      }else{ 
       Log.d("Cursor_info","null"); 
      } 


      try { 
       workout_name_shortener = cursor.getString(1).split("\\s+"); 
       cursor.close(); 
       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++; 
       } 
      }catch (Exception e){ 
       Log.d("Exception_log",""+e); 
      } 



      workout_name = holder + template; 
      //cursor.close(); 
     } 

     return workout_name; 
    } 


    public void Boolean_Generator(String[] boolean_S) { 

     int counter = 0; 

     isDone = new boolean[boolean_S.length]; 
     for (int i = 0; i < boolean_S.length; i++) { 

      if (boolean_S[i].equals("true")) { 
       bool_counter++; 
       isDone[i] = Boolean.TRUE; 

       Tab_Colour.add(getResources().getColor(R.color.siva_3)); 

      } else { 


       if(counter<1){ 
        Tab_Colour.add(getResources().getColor(R.color.siva_1)); 
       }else{ 
        Tab_Colour.add(getResources().getColor(R.color.siva_2)); 
       } 
       counter++; 
       isDone[i] = Boolean.FALSE; 
      } 
     } 

     for (int i = 0; i < isDone.length; i++) { 
      Log.d("bools_a", "" + isDone[i]); 
      if (isDone[i] == true) { 
       current_day++; 
      } 
     } 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       back_pressed(); 
       return true; 
      case R.id.action_renew: 

       Toast.makeText(this, "Plan renewed", Toast.LENGTH_SHORT).show(); 

       dbcon.open(); 

       if(Plan_Name.equals("Newbie")){ 

        dbcon.updatePlanStock("Newbie", "UNDER TEXT", "ic_newbie", "7 Days", "5 8 12 REST 7 REST 1", "false false false false false false false"); 

       }else if(Plan_Name.equals("Alpha 10")){ 

        dbcon.updatePlanStock("Alpha 10", "UNDER TEXT", "ic_alphaten", "14 Days", "1 2 3 4 5 6 7 8 9 10", "false false false false false false false false false false"); 

       }else if(Plan_Name.equals("Alpha V2")){ 

        dbcon.updatePlanStock("Alpha V2", "Pro version", "ic_alphav2", "30 Days", "1 3 11 10 13 REST REST 5 4 11 12 8 REST REST 3 7 13 11 REST 13 12 REST REST 6 12 4 1 REST 12 13", "false false false false false false false false false false false false false false false false false false false false false false false false false false false false false false"); 

       } 

       dbcon.close(); 

       dbcon_l.open(); 
       dbcon_l.insertData("" + Plan_Name + " Plan", "" + date); 
       dbcon_l.close(); 


       Bundle ExerciseBundle = new Bundle(); 
       ExerciseBundle.putString("plan_info", Plan_Name); 

       Intent i = new Intent(/* FirstActivity.this */ 
         getApplicationContext(), 
         PlanAdapter.class); 
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       i.putExtras(ExerciseBundle); 

       startActivity(i); 

       return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 

     if (current_day == isDone.length) { 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu_delete, menu); 
     } 

     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 

     back_pressed(); 

    } 


    public void back_pressed() { 
     Intent test = new Intent(getApplication(), MainActivity.class); 
     test.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(test); 
    } 

} 

その他のログ猫:

プロセス:com.spartanbodyweightworkouts、PID:14142 れるjava.lang。 RuntimeException:アクティビティを開始できません ComponentInfo {com.spartanbodyweightworkouts/com.spartanbodyweightworkouts.tabs.plan.planAdapter.planAdapter.PlanAdapter}: android.database.CursorIndexOutOfBounds例外:インデックス0は android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)アンドロイドで で android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) で0 の大きさで、 を要求しました。 android.os.Handler.dispatchMessageでapp.ActivityThread.-wrap11(ActivityThread.java) でandroid.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) (Handler.java:102) アンドロイドで。 os.Looper.loop(Looper.java:148) android.app.ActivityThread.main(ActivityThread.java:5417) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)com.android.internal.os.ZygoteInitで でjava.lang.reflect.Method.invoke(ネイティブメソッド) で。 android.database.CursorIndexOutOfBoundsException:によって引き起こさメイン(ZygoteInit.java:616) インデックス0 は0 でandroid.database.AbstractCursor.checkPosition(AbstractCursor.java:460) アンドロイドで の大きさで、要求されました。データベース.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database。 AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) ( )com.spartanbodyweightworkouts.tabs.plan.planAdapter.planAdapter.PlanAdapter.Workout_Name(PlanAdapter。Javaの:247) com.spartanbodyweightworkouts.tabs.plan.planAdapter.planAdapter.PlanAdapter.generateWorkouts(PlanAdapter.java:174) でcom.spartanbodyweightworkouts.tabs.plan.planAdapter.planAdapter.PlanAdapter.onCreate(PlanAdapterで。ジャワ: でandroid.app.Activity.performCreate(Activity.java:6251) における109) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) android.app.ActivityThread.performLaunchActivity(ActivityThreadで 。 Javaの:2369) android.app.ActivityThread.handleLaunchActivityで(ActivityThread.jav A:2476)android.os.Handler.dispatchMessage(ハンドラで android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) でandroid.app.ActivityThread.-wrap11(ActivityThread.java) で 。 java:102) (android.os.Looper.loop(Looper.java:148)) (android.app.ActivityThread.main(ActivityThread.java:5417)) at java.lang.reflect.Method.invoke(ネイティブメソッド) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

+0

に変更する必要がありますが、行番号と、関連するlogcatを表示することができますし、それはあなたのコードにある行を教えて? –

+0

確かに私がすることができますが、私は、データベースにデータを追加した後、私は別の部分でそれを使用することができますし、同じカーソルがnullではないカントー問題は、カーソルが空であることを示しています。 –

+1

私はdownvoteを持っている理由は分かりません。 –

答えて

0

問題は、私はテーブルからすべてのレコードを削除する場合ということです、と私はデータを再度追加するとき、それはトレーニングを要求する計画イムでid's.Butを切り替えそのidでexssistではない。

dbcon.open(); 
       dbcon.insertPlanStock("Newbie", "Free", "ic_newbie", "7 Days", "5 8 12 REST 7 REST 1", "false false false false false false false"); 
       dbcon.close(); 

数字がワークアウト名

1

コードの末尾には、cursor.close;がコメントアウトされています。

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

if(cursor != null && cursor.moveToFirst()){ 
.../... 
workout_name = holder + template; 
//cursor.close(); 

あなたもすぐにそれを閉じていると、tryおよびcatchが失敗した場合にも、カーソルがクローズされていることを確認する必要があります。 「if」や「try」の場合には、整った家屋の資源を保つことをお勧めします。


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()){ 
      Log.d("Cursor_info","not_null"); 
      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) + ")"; 
      } 

     }else{ 
      Log.d("Cursor_info","null"); 
     } 


     try { 
      workout_name_shortener = cursor.getString(1).split("\\s+"); 
      cursor.close(); 
      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++; 
      } 
     }catch (Exception e){ 
      Log.d("Exception_log",""+e); 
     } 



     workout_name = holder + template; 
     //cursor.close(); 
    } 

    return workout_name; 
} 

私はあなたのアプリケーションしかし、あなたのsqliteのヘルパーの単一のインスタンスを使用することをお勧めします。詳細はこちらを見てください:

Is it OK to have one instance of SQLiteOpenHelper shared by all Activities in an Android application?

+0

カーソルがint型のカウンターの上に閉じている= 0.Theカーソルがnullである私はそれを編集する必要があり、データベース –

+0

@Mickeyに意味より明確にする。また、メモリリークを避けるためにデータベースヘルパのインスタンスを1つ使用することを検討してください。 –

+0

残念ながら私は英語のネイティブスピーカーではありません。この部分を説明できますか?また、メモリリークを避けるためにデータベースヘルパのインスタンスを1つ使用することを検討してください。 –

関連する問題