Как очистить исключение StringIndexOutOfBoundsException - PullRequest
0 голосов
/ 09 февраля 2019

Я новичок в Android Studio.При запуске кода на телефоне с SDK версии 23 приложение останавливается по непредвиденным причинам.При проверке Logcat отображается ошибка в SQLiteDatabase db = mDbHelper.getReadableDatabase();

  02-09 20:55:17.833 15461-15461/com.example.android.pets E/MultiWindowProxy: getServiceInstance failed!
    02-09 20:55:18.082 15461-15461/com.example.android.pets E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.android.pets, PID: 15461
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pets/com.example.android.pets.CatalogActivity}: java.lang.StringIndexOutOfBoundsException: length=0; index=0
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2682)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:207)
            at android.app.ActivityThread.main(ActivityThread.java:5811)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
         Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=0
            at java.lang.String.charAt(Native Method)
            at android.app.ContextImpl.validateFilePath(ContextImpl.java:1926)
            at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
            at com.example.android.pets.CatalogActivity.displayDatabaseInfo(CatalogActivity.java:68)
            at com.example.android.pets.CatalogActivity.onStart(CatalogActivity.java:61)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1268)
            at android.app.Activity.performStart(Activity.java:6333)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2553)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2682) 
            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512) 
            at android.os.Handler.dispatchMessage(Handler.java:111) 
            at android.os.Looper.loop(Looper.java:207) 
            at android.app.ActivityThread.main(ActivityThread.java:5811) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 

код:

private void displayDatabaseInfo() {
    // To access our database, we instantiate our subclass of SQLiteOpenHelper
    // and pass the context, which is the current activity.
    Cursor cursor;
    try (SQLiteDatabase db = mPetDbHelper.getReadableDatabase()) {

        String[] projection = {PetContract.PetEntry._ID, PetContract.PetEntry.COLUMN_PET_NAME, PetContract.PetEntry.COLUMN_PET_BREED, PetContract.PetEntry.COLUMN_PET_GENDER, PetContract.PetEntry.COLUMN_PET_WEIGHT};

        // Create and/or open a database to read from it


        // Perform this raw SQL query "S  \
        // ELECT * FROM pets"
        // to get a Cursor that contains all rows from the pets table.
        cursor = db.query(PetContract.PetEntry.TABLE_NAME, projection, null, null, null, null, null);
    }
    TextView displayView = findViewById(R.id.text_view_pet);

    try {
        // Display the number of rows in the Cursor (which reflects the number of rows in the
        // pets table in the database).

        displayView.setText("The pets table contains:" + cursor.getCount() + "pets.\n\n");
        displayView.append(PetContract.PetEntry._ID + "-" + PetContract.PetEntry.COLUMN_PET_NAME + "-" + PetContract.PetEntry.COLUMN_PET_BREED + "-" + PetContract.PetEntry.COLUMN_PET_GENDER + "-" + PetContract.PetEntry.COLUMN_PET_WEIGHT);

        int idColummnIndex = cursor.getColumnIndex(PetContract.PetEntry._ID);
        int nameColumnIndex = cursor.getColumnIndex(PetContract.PetEntry.COLUMN_PET_NAME);
        int breedColumnIndex = cursor.getColumnIndex(PetContract.PetEntry.COLUMN_PET_BREED);
        int genderColumnIndex = cursor.getColumnIndex(PetContract.PetEntry.COLUMN_PET_GENDER);
        int weightColumnIndex = cursor.getColumnIndex(PetContract.PetEntry.COLUMN_PET_WEIGHT);

        while(cursor.moveToNext()){

            int currentID = cursor.getInt(idColummnIndex);
            String currentName = cursor.getString(nameColumnIndex);
            String currentBreed = cursor.getString(breedColumnIndex);
            int currentGender = cursor.getInt(genderColumnIndex);
            int currentWeight = cursor.getInt(weightColumnIndex);

            displayView.append("\n" + currentID + "-" + currentName + "-" + currentBreed + "-" +currentGender + "-" + currentWeight);

        }

    } finally {
        // Always close the cursor when you're done reading from it. This releases all its
        // resources and makes it invalid.
        cursor.close();
    }
}`
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...