0

How can you call a method within an AsyncTask? In my asynctask,which is an inner class in my java file 'xyz', when the user clicks a button, it should call a method within 'xyz' which also happens to be an alertDialog, i know it calls it, but when it reaches the method, the app crashes and gives a runtime exception, which says 'Can't create handler inside thread that has not called Looper.prepare()'. I looked up examples here but it threw the same runtime exception. How can i make it work? isn't calling an outer method from within the asynctask a possibility? this is the snippet to call the method:

 private class DownloadFilesTask extends AsyncTask<Void,Void,Void> {

    private LoginActivity loginActivity;

    public DownloadFilesTask(LoginActivity loginActivity){
        this.loginActivity=loginActivity;
    }


    @Override
    protected Void doInBackground(Void... voids) {
        long start=System.currentTimeMillis();
        in=null;
        try {
            website=new URI(URL);
            request.setURI(website);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpPost httpPost=new HttpPost(URL);
        List<NameValuePair>nameValuePairs=new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("name",name));
        nameValuePairs.add(new BasicNameValuePair("pwd",pwd));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {

            response=httpClient.execute(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String line=in.readLine();
            long end=System.currentTimeMillis();
            long times=end-start;
            String time=String.valueOf(times);
            System.out.println(name);
            System.out.println(NAME_PATTERN);
            System.out.println(pwd);
            System.out.println(PWD_PATTERN);
             if (name.equals(NAME_PATTERN) && (pwd.equals(PWD_PATTERN))) {
                bloggedIn=true;
                System.out.println("Youre right");

            }else
            {
                bloggedIn=false;
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void onPostExecute(String line, String time) {
        if(bloggedIn=true){
            navigateToMainActivity(time);
        }else{ if (bloggedIn=false){
            newp(line,time);
        }

        }
    }
}

and this is the method called:

public void navigateToMainActivity(String timetoo) {
    al=new AlertDialog.Builder(LoginActivity.this);
    al.setTitle("Welcome");
    al.setMessage("Hey there");
    al.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            startActivity(new Intent(LoginActivity.this, Main.class));
        }
    });
    al.show();
}
Aria
  • 389
  • 3
  • 7
  • 25
  • You need to add a little more of code. – Ele Nov 10 '15 at 18:14
  • what else do you think I should add? i just added the part of my code relevant to the question @eleazarenrique – Aria Nov 10 '15 at 18:15
  • Where is the snippet that calls the method located in the `asynctask`? – DigitalNinja Nov 10 '15 at 18:16
  • @digitalninja the first snippet is in the asynctask(), the second is the one within the Activity which is called from the asynctask() – Aria Nov 10 '15 at 18:17
  • add your complete AsyncTas codek.. otherwise its not clear what you want to do. Note that you are not able to operate on UI from `doInBackground` and need to do this in `onPostExecute` – user1908375 Nov 10 '15 at 18:18
  • That's what I was trying to get at. That snippet needs to be in the pre or post execution of the async task thread. – DigitalNinja Nov 10 '15 at 18:21
  • @user1908375 I have added the complete AsyncTask code – Aria Nov 10 '15 at 18:24
  • @DigitalNinja I have edited the question – Aria Nov 10 '15 at 18:24
  • Okay, I don't think you want to call `onPostExecute` from within `doInBackground`. `doInBackground` should pass what `onPostExecute` needs when it's finished running, and it will automatically call `onPostExecute` for you. – DigitalNinja Nov 10 '15 at 18:27
  • @digitalninja i edited it. I took off the onPostExecute from the doinBackground, and then i set bloggedIn to true or false, depending on user's options. then in onPostexecute, i put what should happen when bloggedIn is true or false. but when i ran all this, it just stopped at the end of doinBackground() – Aria Nov 10 '15 at 18:33

1 Answers1

1

It looks like you need doInBackground to return true or false when it's finsished. You need doInBackground to return the boolean. Try this:

@Override
protected Boolean doInBackground(Void... arg0)
{
    // your stuff
    return bloggedIn; // instead of null or return the boolean where you are setting it true or false
}

Then your onPostExecute should look like this:

 @Override
 protected void onPostExecute(Boolean result)
 {
    super.onPostExecute(result);
    if(result){
        navigateToMainActivity(time);
    }else{
        newp(line,time);
    }
 }
DigitalNinja
  • 1,078
  • 9
  • 17
  • it doesn't get the strings line and time though. how can i make it get them, cos i would need it in the alertDialog? – Aria Nov 10 '15 at 18:45
  • You could make them global to the task/class or write set and get methods for them. – DigitalNinja Nov 10 '15 at 18:48