Ana
Ana

Reputation: 831

ArrayIndexOutOfBoundException on calling View.draw(Canvas) method

I am trying create bitmap from a Linear Layout in a asynctask(). Please find the code here:

while (running) {
        count++;
        v1.draw(new Canvas(bitmap));//I am getting exception here
        //sleep thread to give some lag
    try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

        System.out.println("Bitmap : [ " + count + " ] >>>>>>>> SAVED. ");

    }

And I am getting the error as:

04-21 16:29:46.768: E/AndroidRuntime(20850): Caused by: java.lang.IndexOutOfBoundsException: 1, 1
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.text.PackedIntVector.getValue(PackedIntVector.java:70)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.text.DynamicLayout.getLineTop(DynamicLayout.java:592)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.text.Layout.getLineForVertical(Layout.java:1015)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.text.Layout.getLineRangeForDraw(Layout.java:460)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.text.Layout.draw(Layout.java:198)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.widget.Editor.onDraw(Editor.java:1306)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.widget.TextView.onDraw(TextView.java:5163)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.view.View.draw(View.java:14465)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.view.View.draw(View.java:14350)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.view.View.draw(View.java:14468)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at com.sample.ScreenShotTask.takeScreenshots(ScreenShotTask.java:75)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at com.sample.ScreenShotTask.doInBackground(ScreenShotTask.java:35)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at com.sample.ScreenShotTask.doInBackground(ScreenShotTask.java:1)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-21 16:29:46.768: E/AndroidRuntime(20850):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-21 16:29:46.768: E/AndroidRuntime(20850):    ... 4 more

Can anybody help?

Upvotes: 1

Views: 1607

Answers (1)

Whisky
Whisky

Reputation: 327

You canuse try catch block for this exception. Like below :

while (running) {
    count++;
    try {
        v1.draw(new Canvas(bitmap));
    } catch(ArrayIndexOutOfBoundException e){
        e.printStackTrace();
    }
    try {
        Thread.sleep(30);
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }

    System.out.println("Bitmap : [ " + count + " ] >>>>>>>> SAVED. ");
}

Upvotes: 1

Related Questions