Issue
I am trying to create an AsyncTask to access my Room database. Here is the class:
public class MyTask extends AsyncTask<Void, Void, Integer> {
private Activity activity;
private int userId;
private String userName;
private String userEmail;
public MyTask(Activity activity ,int userId, String userName, String userEmail) {
this.activity= activity;
this.userId = userId;
this.userName = userName;
this.userEmail = userEmail;
}
@Override
protected Integer doInBackground(Void... voids) {
return null;
}
}
All goes well, i can initialize the class in my fragment from where I access the DB (tried it without a constructor). But if I try to initialize it with constructors (as you can see above) it doesn't want to compile
MyTask myTask = new MyTask(getActivity(),1,"Bob","someEmail").execute();
I assume this is real simple. Alas, google didn't help me on this one and I already have a flimsy understanding of AsyncTask. I just don't understand how adding a constructor changes the type of the instance (from MyTask to AsyncTask).
Solution
The problem is that you are calling execute()
after instantiating your MyTask (execute()
returns AsyncTask as you can read here) .
So change this:
MyTask myTask = new MyTask(getActivity(),1,"Bob","someEmail").execute();
to this:
MyTask myTask = new MyTask(getActivity(),1,"Bob","someEmail");
myTask.execute();
Answered By - kAliert
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.