Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexOutOfBoundsException: charAt: 0 >= length 0 on some versions of Android

I am trying to get into using Java and Eclipse to make applications (currently using B4A, but want to expand my available resources, and ability to make libraries)

When trying to implement a dialog using fragments, I have followed the example here.

I keep getting

05-06 13:40:21.060: E/SensorManager(18538): thread start
05-06 13:40:21.105: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0

I have tried debugging, but the debugger never stops on any error (and I am new to this debugger, so not sure how to figure that one out).

Does anyone see where the exception is being caused? (Just to make sure it wasn't a fat finger anywhere, I downloaded via the git, and tried it out, and received exact same errors in the logcat)

Here is my mainActivity.java:

package com.example.fragmenttest;
import com.example.fragmenttest.EditNameDialog.EditNameDialogListener;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends FragmentActivity  implements EditNameDialogListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_edit_name);
    showEditDialog();
}

private void showEditDialog() {
    FragmentManager fm = getSupportFragmentManager();
    EditNameDialog editNameDialog = new EditNameDialog();
    editNameDialog.show(fm, "fragment_edit_name");
}

@Override
public void onFinishEditDialog(String inputText) {
    Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
}
}

And here is the EditNameDialog.java:

package com.example.fragmenttest;

import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;


public class EditNameDialog extends DialogFragment implements OnEditorActionListener {

public interface EditNameDialogListener {
    void onFinishEditDialog(String inputText);
}

private EditText mEditText;

public EditNameDialog() {
    // Empty constructor required for DialogFragment
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_edit_name, container);
    mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    getDialog().setTitle("Hello");

    // Show soft keyboard automatically
    mEditText.requestFocus();
    getDialog().getWindow().setSoftInputMode(
            LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    mEditText.setOnEditorActionListener(this);

    return view;
}

@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (EditorInfo.IME_ACTION_DONE == actionId) {
        // Return input text to activity
        EditNameDialogListener activity = (EditNameDialogListener) getActivity();
        activity.onFinishEditDialog(mEditText.getText().toString());
        this.dismiss();
        return true;
    }
    return false;
}
}

and my String Resource file:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">fragmentTest</string>
    <string name="hint_value">hint</string>
    <string name="your_name">Your name</string>

</resources>

Complete StackTrace from logcat: (no different than at the top)

05-06 13:40:21.060: E/SensorManager(18538): thread start
05-06 13:40:21.105: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.105: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.110: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.200: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.205: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.205: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.210: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.215: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.275: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.275: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.300: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.300: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.300: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.470: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.470: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.725: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
05-06 13:40:21.725: E/Dynamiclayout(18538): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0

Edit : Was running on my Galaxy S3 running 4.1.2. When tried out on my Galaxy SL running 2.3.4, absolutely no logcat issues. This tells me it is platform related, but in my searches, I did not see anything related to Android version. Follow up question, does anyone know of an issue with JB (or anything else I've mentioned) that might account for this error?

like image 833
user2346305 Avatar asked May 06 '13 07:05

user2346305


1 Answers

Probably, the exception is thrown by android.text.SpannableStringBuilder#charAt(int).

/**
 * Return the char at the specified offset within the buffer.
 */
public char charAt(int where) {
    int len = length();
    if (where < 0) {
        throw new IndexOutOfBoundsException("charAt: " + where + " < 0");
    } else if (where >= len) {
        throw new IndexOutOfBoundsException("charAt: " + where + " >= length " + len);
    }

    if (where >= mGapStart)
        return mText[where + mGapLength];
    else
        return mText[where];
}

I guess some kind of thread-unsafe operation is performed in your code.

For example, it may be worth trying to move showEditDialog() from onCreate(Bundle) to onResume(). If it does not solve the issue, EditNameDialog#onCreateView is the next to check.

Android API has many onCreateXxx methods, and there are documented and undocumented things we should avoid doing within the methods.

like image 75
Takahiko Kawasaki Avatar answered Nov 08 '22 21:11

Takahiko Kawasaki