Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:13:22+00:00 2026-06-03T10:13:22+00:00

I am getting this exception. When, i’m trying to run my program. In my

  • 0

I am getting this exception. When, i’m trying to run my program. In my program i am executing an AsyncTask For each download and upload tasks, I have created new AsyncTask object and tried to run it in background. I tried to find my mistake. But i can’t find where i am going wrong. Below are my stack trace and code of my program –

05-07 10:00:44.899: E/AndroidRuntime(367): FATAL EXCEPTION: AsyncTask #1
05-07 10:00:44.899: E/AndroidRuntime(367): java.lang.RuntimeException: An error occured while executing doInBackground()
05-07 10:00:44.899: E/AndroidRuntime(367):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.lang.Thread.run(Thread.java:1019)
05-07 10:00:44.899: E/AndroidRuntime(367): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-07 10:00:44.899: E/AndroidRuntime(367):  at android.os.Handler.<init>(Handler.java:121)
05-07 10:00:44.899: E/AndroidRuntime(367):  at android.widget.Toast.<init>(Toast.java:68)
05-07 10:00:44.899: E/AndroidRuntime(367):  at android.widget.Toast.makeText(Toast.java:231)
05-07 10:00:44.899: E/AndroidRuntime(367):  at com.android.test.Helper.downloadTask(t.java:269)
05-07 10:00:44.899: E/AndroidRuntime(367):  at com.android.test.Helper.doInBackground(t.java:132)
05-07 10:00:44.899: E/AndroidRuntime(367):  at com.android.test.Helper.doInBackground(t.java:1)
05-07 10:00:44.899: E/AndroidRuntime(367):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-07 10:00:44.899: E/AndroidRuntime(367):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-07 10:00:44.899: E/AndroidRuntime(367):  ... 4 more

This is my AsynTask Helper

class Helper extends AsyncTask<Integer, Void, Void> 
{
    private SmbFile dfile;
    private String dfilepath;
    private SmbFile dfolder;
    private String dfolderpath;
    private File ufolder;
    private SmbFile ufoldersmb;
    private NtlmPasswordAuthentication auth;
    private int upstate=0;
    private int downstate=0;
    private Context context;
    private ArrayList<SmbFile> smbArray=new ArrayList<SmbFile>();
    private String ext;
    private int tasknumber;
    private String taskname;
    public Context getcontext()
    {
        return context;
    }
    public int gettasknumber()
    {
        return tasknumber;
    }
    public String gettaskname()
    {
        return taskname;
    }
    public int getupstate() 
    {
        return upstate;
    }
    public int getdownstate() 
    {
        return downstate;
    }
    public void setdfile(SmbFile a) 
    {
        this.dfile = a;
    }
    public void setdfilepath(String b) 
    {
        this.dfilepath = b;
    }
    public void setdfolder(SmbFile c) 
    {
        this.dfolder = c;
    }
    public void setdfolderpath(String d) 
    {
        this.dfolderpath = d;
    }
    public void setufolder(File g) 
    {
        this.ufolder = g;
    }
    public void setufoldersmb(SmbFile h) 
    {
        this.ufoldersmb = h;
    }
    public void setauthentication(NtlmPasswordAuthentication i) 
    {
        this.auth = i;
    }
    public void setupstate(int j) 
    {
        upstate = j;
    }
    public void setdownstate(int k) 
    {
        downstate = k;
    }
    public void setcontext(Context l) 
    {
        context = l;
    }
    public void setarraysmb(SmbFile m) 
    {
        this.smbArray.add(m);
    }
    public void setextstorage(String n) 
    {
        this.ext=n;
    }
    public void settasknumber(int o) 
    {
        this.tasknumber=o;
    }
    public void settaskname(String p) 
    {
        this.taskname=p;
    }
    @Override
    protected Void doInBackground(Integer... params) 
    {
        //check flag to execute exactly method
        switch (params[0]) 
        {
            case 0:
                downloadTask();
                break;
            case 1:
                Toast.makeText(context, "AccessPC Upload task "+tasknumber+" Started", Toast.LENGTH_LONG).show();
                uploadFolder(ufolder,ufoldersmb);
                break;
            default:break;
        }
        return null;
    }
    void downloadFile(SmbFile dfile,String dpath)
    {   
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); 
        long blockSize = statFs.getBlockSize();
        long freeSize = statFs.getFreeBlocks()*blockSize;
        try
        {
            if(!((freeSize-dfile.length())<0))
            {
                SmbFileInputStream din=new SmbFileInputStream(dfile);
                FileOutputStream dout=new FileOutputStream(dpath);
                int c;
                while((c=din.read())!=-1)
                {
                    dout.write(c);
                }
                if (din != null) 
                {
                    din.close();
                }
                if (dout != null) 
                {
                    dout.close();
                }
            }
            else
            {
                Toast.makeText(context, "AccessPC Download Task failed ",Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
            Toast.makeText(context, "AccessPC Download Task failed "+e,Toast.LENGTH_LONG).show();
        }
    }
    void downloadFolder(SmbFile dfolder,String dfolderpath)
    {
        try 
        {
            dfolderpath=dfolderpath+dfolder.getName();
            if(!(new File(dfolderpath)).exists())
            {
                (new File(dfolderpath)).mkdir();
            }
            SmbFile[] temp=dfolder.listFiles(); 
            if(temp.length==0)
            {
                Toast.makeText(context, "df,downstate="+downstate,Toast.LENGTH_LONG).show();
                return;
            }
            for(SmbFile m:temp)
            {
                if(m.isFile())
                {
                    downloadFile(m,dfolderpath+m.getName());
                }
                else
                {
                    downloadFolder(m,dfolderpath);
                }
            }
        } 
        catch (Exception e) 
        {
            Toast.makeText(context, "AccessPC Download Task failed "+e,Toast.LENGTH_LONG).show();
        }
    }
    void uploadFile(File ufile,SmbFile ufilesmb)
    {
        try
        {
            FileInputStream uin=new FileInputStream(ufile);
            SmbFile tempSmb=new SmbFile(ufilesmb.getPath()+ufile.getName(),auth);
            SmbFileOutputStream uout=new SmbFileOutputStream(tempSmb);
            int c;
            while((c=uin.read())!=-1)
            {
                uout.write(c);
            }
            if (uin != null)
            {
                uin.close();
            }
            if (uout != null) 
            {
                uout.close();
            }
        }
        catch(Exception e)
        {
            Toast.makeText(context, "AccessPC Upload Task failed "+e, Toast.LENGTH_LONG).show();
        }
    }
    void uploadFolder(File ufolder,SmbFile ufoldersmb)
    {
        try 
        {
            SmbFile tempSmb=new SmbFile(ufoldersmb.getPath()+ufolder.getName()+"/",auth);
            if(!tempSmb.exists())
            {
                tempSmb.mkdir();
            }
            File[] ftemp=ufolder.listFiles();
            if(ftemp.length==0)
            {
                setupstate(2);
                return;
            }
            for(File m:ftemp)
            {
                if(m.isFile())
                {
                    uploadFile(m,tempSmb);
                }
                else
                {
                    uploadFolder(m,tempSmb);
                }
            }
        } 
        catch (Exception e) 
        {
            Toast.makeText(context, "AccessPC Upload Task failed "+e,Toast.LENGTH_LONG).show();
        }
    }
    void downloadTask()
    {
        Toast.makeText(context, "AccessPC download task "+tasknumber+" Started", Toast.LENGTH_LONG).show();
        try 
        {
            for(SmbFile m:smbArray)
            {
                if(m.isFile())
                {       
                    setdfile(m);
                    setdfilepath(ext+m.getName());
                    downloadFile(dfile,dfilepath);
                }
                else
                {
                    setdfolder(m);
                    setdfolderpath(ext);
                    downloadFolder(dfolder,dfolderpath);
                }
            } 
            setdownstate(2);
        }
        catch (Exception e) 
        {
            Toast.makeText(context,"Download error "+e,Toast.LENGTH_LONG).show();
        }
    }   
    @Override
    protected void onPostExecute(Void result) 
    {
        if(upstate==2)
        {
            setupstate(0);
            Toast.makeText(context, "AccessPC "+taskname+" task "+tasknumber+" Finished", Toast.LENGTH_LONG).show();
        }
        if(downstate==2)
        {
            setdownstate(0);
            Toast.makeText(context, "AccessPC "+taskname+" task "+tasknumber+" Finished", Toast.LENGTH_LONG).show();
        }   
    }
}

This is my options menu. I ve used this to do my asyn task
variable i have used in my main activity which helps this task:

String extStorage=Environment.getExternalStorageDirectory()+"/t/";
ArrayList<Helper> helpobject=new ArrayList<Helper>();
int uptask=0;
int downtask=0;
public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch(item.getItemId())
        {
            case DOWNLOAD1:
                            MENU_STATE=MENU_DOWNLOAD;
                            map=display_check(current);
                            return true;
            case UPLOAD:
                            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                            {
                                if(current.getShare()==null)
                                {
                                    Toast.makeText(this,"UPLOAD FAILED",Toast.LENGTH_LONG).show();
                                }
                                else
                                {
                                    File f=new File(extStorage);
                                    Helper help=new Helper();
                                    help.setufolder(f);
                                    help.setufoldersmb(current);
                                    help.setauthentication(auth);
                                    help.setupstate(1);
                                    help.settasknumber(uptask);
                                    uptask++;
                                    help.settaskname("Upload");
                                    help.setcontext(this.getApplicationContext());
                                    help.execute(1);
                                }
                            }
                            else
                            {
                                Toast.makeText(this,"UPLOAD FAILED--NO SD CARD FOUND",Toast.LENGTH_LONG).show();
                            }
                            return true;
        case DELETE1:
                            MENU_STATE=MENU_DELETE;
                            map=display_check(current);
                            return true;
        case QUIT:
                            int x=0;
                            for(Helper k:helpobject)
                            {
                                if(k.getStatus().equals(AsyncTask.Status.RUNNING)||k.getStatus().equals(AsyncTask.Status.RUNNING))
                                {
                                    Toast.makeText(k.getcontext(), "AccessPC "+k.gettaskname()+" "+k.gettasknumber()+" Cancelled", Toast.LENGTH_SHORT).show();
                                    k.cancel(true);
                                }
                                helpobject.remove(x);
                                x++;
                            }
                            return true;
        case DOWNLOAD2:  
                            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                            {
                                int tempcount=0;
                                for(int i=0;i<object.getCount();i++)
                                {
                                    if(object.getter(i)==true)
                                    {
                                        tempcount++;
                                    }
                                }
                                if(tempcount==0)
                                {
                                    Toast.makeText(this,"Please choose atleast one item for download!!",Toast.LENGTH_LONG).show();
                                }
                                else
                                {
                                    Helper help=new Helper();
                                    helpobject.add(help);
                                    help.settasknumber(downtask);
                                    downtask++;
                                    help.settaskname("Download");
                                    help.setcontext(this.getApplicationContext());
                                    help.setextstorage(extStorage);
                                    help.setdownstate(1);
                                    for(int i=0;i<object.getCount();i++)
                                    {
                                        if(object.getter(i)==true)
                                        {
                                            help.setarraysmb(map.get(object.getItem(i)));
                                        }
                                    }
                                    help.execute(0);
                                }
                            }
                            else
                            {
                                Toast.makeText(this,"DOWNLOAD FAILED--NO SD CARD FOUND",Toast.LENGTH_LONG).show();
                            }                   
                            return true;
        case DELETE2:
                            for(int i=0;i<object.getCount();i++)
                            {
                                if(object.getter(i)==true)
                                {
                                    try 
                                    {
                                        map.get(object.getItem(i)).delete();
                                    } 
                                    catch (Exception e) 
                                    {
                                        Toast.makeText(this,"cannot be deleted "+e,Toast.LENGTH_LONG).show();
                                    }
                                }
                            }
                            return true;
        case CANCEL:
                            MENU_STATE=MENU_GENERAL;
                            map=display(current);
                            return true;
        case FINISH:
                            finish();
        default:
                            return super.onOptionsItemSelected(item);
        }
    }

hi,when i tried to display the toast, as mentioned in one of the below comments, ive got the same exception .please see the code below whih i ve used to dispaly Toast in my async task:

(new Activity()).runOnUiThread(new Runnable(){@Override public void run() {//Your Toast
            Toast.makeText(context, "AccessPC Download Task failed ",Toast.LENGTH_LONG).show();
            }});

Do i need to make any changes to my async task code or options menu code?

I ve used handler like this:

public Handler handler = new Handler() {
    public void handleMessage(Message msg) 
    {
        Bundle b = msg.getData();
        String key = b.getString(null);
        Toast.makeText(getApplicationContext(),key, Toast.LENGTH_SHORT).show();
    }
};

and i am calling handler like this:

Message msg = new Message();
Bundle b = new Bundle();
b.putString(null, "AccessPC "+taskname+" task "+tasknumber+" Finished");
msg.setData(b);
//this is error
t.handler.sendMessage(msg);

But how can i call a non-static method?do i need to create an object for my main class(class t extends list activity)?

Hi, please see another exception that ive got when i select ‘QUIT’ operation from options menu for the above async task and menu ive posted. how to avoid this exception:

05-07 16:24:07.573: E/AndroidRuntime(13466): FATAL EXCEPTION: main
05-07 16:24:07.573: E/AndroidRuntime(13466): java.util.ConcurrentModificationException
05-07 16:24:07.573: E/AndroidRuntime(13466):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.test.t.onOptionsItemSelected(t.java:622)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.app.Activity.onMenuItemSelected(Activity.java:2205)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.view.View$PerformClick.run(View.java:9080)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.os.Handler.handleCallback(Handler.java:587)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.os.Looper.loop(Looper.java:123)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at android.app.ActivityThread.main(ActivityThread.java:3683)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at java.lang.reflect.Method.invoke(Method.java:507)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-07 16:24:07.573: E/AndroidRuntime(13466):    at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:13:24+00:00Added an answer on June 3, 2026 at 10:13 am

    Can't create handler inside thread that has not called Looper.prepare(), the error message is rather straightforward.

    You created a Handler object in your AsyncTask.doInBackground()(a background thread), which requires a message pump in that thread, that’s the Looper.prepare() thing.

    I think it may be easier to just move the creation of your Handler object out of AysncTask, to the UI thread.

    Below is the edit after the OP edited his/her question

    Toast.makeText(context, "AccessPC Upload task "+tasknumber+" Started", Toast.LENGTH_LONG).show();
    

    The line above is simply not allowed in a background thread, showing a Toast message is something updating the UI, which must happen in the UI thread.

    EDIT again

    You can use a Handler, which is created in the UI thread, to send a message, and in the Handler, handle that message by showing your desired Toast message.

    EDIT again 2

    private final static int TOAST_MSG_ID_1 = 1;
    private final static int TOAST_MSG_ID_2 = 2;
    private final static int TOAST_MSG_ID_3 = 3;
    // declare a Handler as an instance variable
    Handler msgHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
                switch(msg.what()) {
                    case TOAST_MSG_ID_1:
                        Toast.makeText(...msg1...).show();
                    break;
                    case TOAST_MSG_ID_2:
                        Toast.makeText(...msg2...).show();
                    break;
                    .... other messages follow....
                }
        }
    };
    
    // and you will use the following to show a Toast message:
    msgHandler.sendEmptyMessage(TOAST_MSG_ID_1);
    msgHandler.sendEmptyMessage(TOAST_MSG_ID_2);
    msgHandler.sendEmptyMessage(TOAST_MSG_ID_3);
    ...
    

    For more info about Handler, take a look at the javadoc first. and google will bring you more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I am getting this exception when trying to run the query Column
Trying to run z3c.form.testing based code on Plone 4.1 and getting this exception Traceback
I'm getting this exception when I try to run this program. It's one of
I'm trying to parse and load an XML document, however I'm getting this exception
This is the exception that I am getting when I run my application with
I am getting this exception when trying to save a file: System.Web.HttpException: The SaveAs
we're getting this exception sometimes when trying to write a file to the Blobstore.
I am getting this exception while trying to call SOAP webservice using axis. basically
I'm getting this exception when trying to use dbgeng from mdbglib : First-chance exception
I have one TClientDataSet with several Fields and I'm getting this exception, I'm sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.