Android AsyncTask doInBackground操作突然停止,出现未处理的异常

时间:2012-01-26 06:51:27

标签: android

我遇到了一个奇怪的问题。我的 asyncTask操作突然被一个未处理的异常终止。大约45%的任务正在完成然后终止。

这是我的logcat

01-26 12:13:48.216: I/System.out(4421): success
01-26 12:13:48.386: I/System.out(4421): success
01-26 12:13:48.576: I/System.out(4421): success
01-26 12:13:48.616: D/dalvikvm(4421): GC_CONCURRENT freed 473K, 48% free 3256K/6215K, external 0K/0K, paused 3ms+4ms
01-26 12:13:48.806: I/System.out(4421): success
01-26 12:13:48.806: W/dalvikvm(4421): threadid=9: thread exiting with uncaught exception (group=0x40018560)
01-26 12:13:48.806: E/AndroidRuntime(4421): FATAL EXCEPTION: AsyncTask #1
01-26 12:13:48.806: E/AndroidRuntime(4421): java.lang.RuntimeException: An error occured while executing doInBackground()
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.lang.Thread.run(Thread.java:1019)
01-26 12:13:48.806: E/AndroidRuntime(4421): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.ArrayList.get(ArrayList.java:311)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.gesture.Instance.temporalSampler(Instance.java:91)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.gesture.Instance.createInstance(Instance.java:76)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.gesture.GestureStore.addGesture(GestureStore.java:158)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.gesture.GestureLibrary.addGesture(GestureLibrary.java:68)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at com.android.pack.TestActivity.saveGesture(TestActivity.java:467)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at com.android.pack.TestActivity$A.doInBackground(TestActivity.java:102)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at com.android.pack.TestActivity$A.doInBackground(TestActivity.java:1)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-26 12:13:48.806: E/AndroidRuntime(4421):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-26 12:13:48.806: E/AndroidRuntime(4421):     ... 4 more
01-26 12:13:49.356: E/WindowManager(4421): Activity com.android.pack.TestActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053a358 that was originally added here
01-26 12:13:49.356: E/WindowManager(4421): android.view.WindowLeaked: Activity com.android.pack.TestActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4053a358 that was originally added here
01-26 12:13:49.356: E/WindowManager(4421):  at android.view.ViewRoot.<init>(ViewRoot.java:259)
01-26 12:13:49.356: E/WindowManager(4421):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
01-26 12:13:49.356: E/WindowManager(4421):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-26 12:13:49.356: E/WindowManager(4421):  at android.view.Window$LocalWindowManager.addView(Window.java:465)
01-26 12:13:49.356: E/WindowManager(4421):  at android.app.Dialog.show(Dialog.java:241)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.pack.TestActivity.addgestures(TestActivity.java:159)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.pack.TestActivity.onOptionsItemSelected(TestActivity.java:404)
01-26 12:13:49.356: E/WindowManager(4421):  at android.app.Activity.onMenuItemSelected(Activity.java:2205)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:776)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
01-26 12:13:49.356: E/WindowManager(4421):  at android.view.View$PerformClick.run(View.java:9107)
01-26 12:13:49.356: E/WindowManager(4421):  at android.os.Handler.handleCallback(Handler.java:587)
01-26 12:13:49.356: E/WindowManager(4421):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 12:13:49.356: E/WindowManager(4421):  at android.os.Looper.loop(Looper.java:130)
01-26 12:13:49.356: E/WindowManager(4421):  at android.app.ActivityThread.main(ActivityThread.java:3835)
01-26 12:13:49.356: E/WindowManager(4421):  at java.lang.reflect.Method.invokeNative(Native Method)
01-26 12:13:49.356: E/WindowManager(4421):  at java.lang.reflect.Method.invoke(Method.java:507)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
01-26 12:13:49.356: E/WindowManager(4421):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
01-26 12:13:49.356: E/WindowManager(4421):  at dalvik.system.NativeStart.main(Native Method)
01-26 12:14:04.836: I/Process(4421): Sending signal. PID: 4421 SIG: 9

,这是AsyncTask类

class A extends AsyncTask<File[],Integer,Void>
        {
            private Context cnt;
                int count=0;
            @Override
            protected void onPreExecute() {

            }
            A(Context context)
            {
                cnt=context;
            }

            protected Void doInBackground(File[]... params) {
                // TODO Auto-generated method stub
                File[] newfiles=params[0];
                File[] temp=null;
                int progress=0;
                float dircnt=0;
                int numberofdir=newfiles.length;
                for(File b:newfiles)
                {


                    count=0;
                    dirname=b.getName();
                    if(b.isDirectory())
                    {

                        temp=b.listFiles();
                    }
                    for(File a:temp)
                    {
                        gest=dotask.batchprocess(a.getPath());
                        if(gest!=null)
                            System.out.println("success");
                        String name=dirname+"_"+count;
                        saveGesture(name, gest);
                        count++;

                    }
                    dircnt=dircnt+1;
                     progress=(int)((dircnt/numberofdir)*100);
                     try {
                        Thread.sleep(60);
                        publishProgress(progress);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                }


                return null;
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                // increment progress bar by progress value
                pd.setProgress(values[0]);
                pd.setMessage("completed "+dirname);


        }
             protected void onPostExecute() {
                pd.hide();
                pd=null;
             }

}

这是调用Asynctask对象并执行它的方法

public void addgestures()
        {
            cView.postInvalidate();
            dotask.setH(cView.getH());
            dotask.setW(cView.getW());

            count=0;
            files=null;
            /*if(dir.isDirectory()) 
            {
                files=dir.listFiles();
            }*/
            files=dir.listFiles();
            for(int i=0;i<files.length;i++)
            {
                System.out.println(files[i].getPath());
            }
             pd = new ProgressDialog(this);
                pd.setMessage("Matching progress");
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setMax(100);
                pd.setCancelable(false);
                pd.setProgress(0);
                pd.show(); 



            A a=new A(this);
            a.execute(files);



        }

程序的其余部分没有错误,因为大约40%的任务正在成功完成。所以我不会上传程序的其余部分。

**here is the TESTACTIVITY CLASS**

package com.android.pack;


import java.io.File;
import java.net.URISyntaxException;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class TestActivity extends Activity {
    /** Called when the activity is first created. */
     int height;
     int weight;
     private ProgressDialog pd=null;
     customview cView;
     String path="";
     String pathchk="";
     String temp="";
     String name=null;
     float pathSmooth[];
     private  File mStoreFile;
     Gesture gest;
     File dir;
     String dirname;
     File[] files;
     int count;
     int Height;
     int Weight;
     task dotask;
     Context ct;
     private static GestureLibrary sStore=null;


     private static final int FILE_SELECT_CODE = 0;


        public void perform() //
        {
            cView.set_file(pathchk);
            cView.postInvalidate();
        }
        class A extends AsyncTask<File[],Integer,Void>
        {
            private Context cnt;
                int count=0;
            @Override
            protected void onPreExecute() {

            }
            A(Context context)
            {
                cnt=context;
            }

            protected Void doInBackground(File[]... params) {
                // TODO Auto-generated method stub
                File[] newfiles=params[0];
                File[] temp=null;
                int progress=0;
                float dircnt=0;
                int numberofdir=newfiles.length;
                for(File b:newfiles)
                {


                    count=0;
                    dirname=b.getName();
                    if(b.isDirectory())
                    {

                        temp=b.listFiles();
                    }
                    for(File a:temp)
                    {
                        gest=dotask.batchprocess(a.getPath());
                        if(gest!=null)
                            System.out.println("success");
                        String name=dirname+"_"+count;
                        saveGesture(name, gest);
                        count++;

                    }
                    dircnt=dircnt+1;
                     progress=(int)((dircnt/numberofdir)*100);
                     try {
                        Thread.sleep(60);
                        publishProgress(progress);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }



                }


                return null;
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                // increment progress bar by progress value
                pd.setProgress(values[0]);
                pd.setMessage("completed "+dirname);


        }
             protected void onPostExecute() {
                pd.hide();
                pd=null;
             }
        }
        public void addgestures()
        {
            cView.postInvalidate();
            dotask.setH(cView.getH());
            dotask.setW(cView.getW());

            count=0;
            files=null;
            /*if(dir.isDirectory()) 
            {
                files=dir.listFiles();
            }*/
            files=dir.listFiles();
            for(int i=0;i<files.length;i++)
            {
                System.out.println(files[i].getPath());
            }
             pd = new ProgressDialog(this);
                pd.setMessage("Matching progress");
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setMax(100);
                pd.setCancelable(false);
                pd.setProgress(0);
                pd.show(); 



            A a=new A(this);
            a.execute(files);



        }





         public void showFileChooser() {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
           // File file = new File("/sdcard");
            intent.setType("file://*"); 

            intent.addCategory(Intent.CATEGORY_OPENABLE);

            try {
                startActivityForResult(
                        Intent.createChooser(intent, "Select a File to Upload"),
                        FILE_SELECT_CODE);

            } catch (android.content.ActivityNotFoundException ex) {
                // Potentially direct the user to the Market with a Dialog
                Toast.makeText(this, "Please install a File Manager.", 
                        Toast.LENGTH_SHORT).show();
            }
        }
            public void smoothPoint()
            {

                float pathScale[]=cView.getPathSmooth();
                int pi;             // Index over all the points in the trace

                int ti;             // Index over all the traces of a tracegroup
                float Xavg,Yavg;
                //int numTraces=getNumTraces();
                int numTraces=1;
                int stopCount=0;
                int numPoints =(pathScale.length)/2;
                float xsd,ysd;
                int count1=1,count2=1;
                float m2=0,m1=0;
                float sumX=0, sumY=0;          //partial sum
                float Xchannel []=new float[numPoints*2];
                float Xdist=0,Ydist=0;
                float Ychannel []=new float[numPoints*2];
                float nXchannel[]= new float[numPoints*2*2];
                float nYchannel[]=new float[numPoints*2*2];
                int finalCount=1;
                float n1Xchannel[]= new float[numPoints*4*2];
                float n1Ychannel[]=new float[numPoints*4*2];
                float xfinal[] = new float[numPoints*4*2];
                float yfinal[]= new float[numPoints*4*2];
                int valid[]=new int[numPoints];
                Log.v("number of points",numPoints+"");
                for(int i=0;i<numPoints;i++)
                {
                    //valid[i]=1;
                    Xchannel[i]=pathScale[i*2];
                    Ychannel[i]=pathScale[i*2+1];
                }
                for(ti=0;ti<numTraces;ti++)
                {
                    sumX=sumY=0;
                    for(pi=0;pi<numPoints-1;pi++)
                    {
                        sumX+=Math.abs((Math.abs(Xchannel[pi+1])-Math.abs(Xchannel[pi])));
                        sumY+=Math.abs((Math.abs(Ychannel[pi+1])-Math.abs(Ychannel[pi])));
                    }
                }
                for(pi=1;pi<numPoints-1;pi++)
                {
                    m1=(Ychannel[pi]-Ychannel[pi-1])/(Xchannel[pi]-Xchannel[pi-1]);
                    m2=(Ychannel[pi+1]-Ychannel[pi])/(Xchannel[pi+1]-Xchannel[pi]); 
                    if(m1*m2<0)
                    {
                        valid[pi]=0;
                        stopCount++;

                    }

                }


                nXchannel[0]=Xchannel[0];
                nYchannel[0]=Ychannel[0];
                for(pi=1;pi<numPoints;pi++)
                {
                    nXchannel[pi*2-1]=(Xchannel[pi]+Xchannel[pi-1])/2;

                    nYchannel[pi*2-1]=(Ychannel[pi]+Ychannel[pi-1])/2;
                    //System.out.print(" \n mid x and y "+nXchannel[pi*2-1]+nYchannel[pi*2-1]);
                    nXchannel[pi*2]=Xchannel[pi];

                    nYchannel[pi*2]=Ychannel[pi];
                    count1+=2;
                }
                //System.out.println("new array... ");
                n1Xchannel[0]=nXchannel[0];
                n1Ychannel[0]=nYchannel[0];
                for(pi=1;pi<count1;pi++)
                {
                    n1Xchannel[pi*2-1]=(nXchannel[pi]+nXchannel[pi-1])/2;

                    n1Ychannel[pi*2-1]=(nYchannel[pi]+nYchannel[pi-1])/2;
                    //System.out.print(" \n mid x and y "+n1Xchannel[pi*2-1]+n1Ychannel[pi*2-1]);
                    n1Xchannel[pi*2]=nXchannel[pi];
                    count2+=2;
                    n1Ychannel[pi*2]=nYchannel[pi];

                }
                Log.v("Count1",count1+"");
                Log.v("Count2",count2+"");
                xfinal[0]=n1Xchannel[0];
                yfinal[0]=n1Ychannel[0];
                Xavg=sumX/(numPoints -1);
                Log.v("finalCount",finalCount+"");
                Log.v("Xavg",sumX+"");
                Yavg=sumY/(numPoints-1) ;

                for(pi=1;pi<count2-1;pi++)
                {

                    Xdist=Math.abs((Math.abs(n1Xchannel[pi])-Math.abs(xfinal[finalCount-1])));
                    Ydist=Math.abs((Math.abs(n1Ychannel[pi])-Math.abs(yfinal[finalCount-1])));
                        if(Xdist>=Xavg||Ydist>=Yavg)
                        {
                            xfinal[finalCount]=n1Xchannel[pi];
                            yfinal[finalCount++]=n1Ychannel[pi];
                    }


                }
                pathSmooth=new float[finalCount*2];
                for(pi=0;pi<finalCount;pi++)
                {
                        pathSmooth[pi*2] = xfinal[pi];
                        pathSmooth[pi*2+1] = yfinal[pi];
                    //pnts.get(pi).x=(int) nXchannel[pi];
                    //pnts.get(pi).y=(int) nYchannel[pi];
                    ////System.out.println("x "+(int)nXchannel[pi]+ " and y "+(int)nYchannel[pi]);
                }   

                cView.setPathSmooth(pathSmooth);





        }
     protected void onActivityResult(int requestCode, int resultCode, Intent data)
     {
            switch (requestCode) {
                case  FILE_SELECT_CODE:   
                if (resultCode == RESULT_OK) {  
                    // Get the Uri of the selected file 
                    Uri uri = data.getData();
                    Log.v("check", "File Uri: " + uri.toString());
                    // Get the path

                    try {
                        path = FileUtils.getPath(this, uri);
                        pathchk=path.substring(12);
                        perform();



                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        Log.v("error", "File Path: "+pathchk);
                    }
                    Log.v("check", "File Path: "+path);
                    // Get the file instance
                    // File file = new File(path);
                    // Initiate the upload
                }           
                break;  
                case 1:
                    if(resultCode==RESULT_OK)
                    {
                            Uri uri=data.getData();
                            dir=new File(uri.getPath());
                            dirname=dir.getName();
                            //addgestures();

                    }
                    break;
            }

        super.onActivityResult(requestCode, resultCode, data);
        }
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        cView = (customview)findViewById(R.id.customview1);        
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        mStoreFile= new File(Environment.getExternalStorageDirectory(), "gestures");
        if (sStore == null) {
            sStore = GestureLibraries.fromFile(mStoreFile);
        }
        dotask=new task();

        //select=(Button)findViewById(R.id.button1);      
    }
    public boolean onCreateOptionsMenu(Menu menu)
    {
    //call the base class to include system menus
        super.onCreateOptionsMenu(menu);
        menu.add(0,1,0,"Open File"); // title
        menu.add(0,2,1,"Smoothen");
        menu.add(0,3,2,"add to gesture library");
        menu.add(0,4,3,"Assign a name");
        menu.add(0,5,4,"select a folder");
        menu.add(0,6,5,"Exit");
    //It is important to return true to see the menu
    return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {
        case 1:
            showFileChooser();
            return true;
        case 2:
            smoothPoint();
            return true;
        case 3:

            /*if(name.length()<1)
                Toast.makeText(getApplicationContext(), "Give a name to the gesture ist", Toast.LENGTH_LONG).show();
            else
            {
                gest=cView.batchprocess(pathchk);
                saveGesture(name, gest);
            }*/
            addgestures();
             return true;

        case 4:
            nameDialog();
            return true;
        case 5:
            Intent intent = new Intent("org.openintents.action.PICK_DIRECTORY");
            intent.setData(Uri.parse("file:///sdcard"));
            intent.putExtra("org.openintents.extra.TITLE", "Please select a folder");
            intent.putExtra("org.openintents.extra.BUTTON_TEXT", "Use this folder");
            startActivityForResult(intent, 1);
            return true;
        case 6:
            Toast.makeText(getApplicationContext(), "exiting", Toast.LENGTH_LONG).show();
            System.exit(1);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    public void nameDialog()
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Title");
        alert.setMessage("Message");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
         name = input.getText().toString();
         //Log.d("name",name);
         check();
          // Do something with value!
          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

        alert.show();
    }
    public static GestureLibrary get()
    {
        return sStore;
    }
    public void check()
    {
        if(name.length()<1)
            Toast.makeText(this,"No name is set for this gesture",Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this,"GESTURE NAME IS "+name,Toast.LENGTH_SHORT).show();
    }
    public void saveGesture(String name,Gesture gest)
    {
        final GestureLibrary store = get();
        store.addGesture(name, gest);
        store.save();

    }

}

2 个答案:

答案 0 :(得分:0)

它是否处理目录为空的用例?也许它可能会失败 - 让索引超出约束异常

答案 1 :(得分:0)

在这里查看Instance.java的代码:Instance.java

您的代码在以下行中失败:

float[] pts = GestureUtils.temporalSampling(gesture.getStrokes().get(0), SEQUENCE_SAMPLE_SIZE);

看起来你添加的手势没有任何中风(我不知道手势)并导致IndexOutOfBounds异常。也许这有帮助...