So I was having a bug
where my code was crashing because I am calling methods in my outer
class in onPostExecute
.
So this is my simple class:
public class UsersList extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
}
public void callMe(){
Toast.makeText(getContext(), "I work", Toast.LENGTH_LONG).show();
}
public class UserRecommend extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(String data) {
// The line below crashes with NullPointerException
UsersList.this.callMe();
}
}
}
So UserRecommend
is an inner class in UsersList
, and so I am calling the callMe()
method and it is crashing. It is crashing for any method in my outer
class which is UsersList
. I know I can print toast
directly, but I don't want to as I have a more complex method. How do I fix this, I need to call a method in the outer
class
EDIT:
I found the main bug
- but I don't know how to fix it. Basically, I have a custom recyclerview adapter and that cardview
has a recommend
button. When the user clicks the recommend
button - it invokes a method which handles the db
stuff and recalls a populate
method. I feel that this is where the problem lies but here is the bit where I define the button
action:
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
holder.recommendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UsersList usersList = new UsersList();
UsersList.UsersRecommend usersRecommend = usersList.new UsersRecommend(usersList);
usersRecommend.execute();
}
});
}
That above method is in my CustomAdapter
class and that button
is defined in that class and not my main UsersList
class
Stack trace:
java.lang.NullPointerException
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
at lukazs.usersapp.UsersList.getRView(UsersList.java:186)
at lukazs.usersapp.UsersList.callMe(UsersList.java:161)
at lukazs.usersapp.UsersList$UserRecommend.onPostExecute(UsersList.java:459)
at lukazs.usersapp.UsersList$UserRecommend.onPostExecute(UsersList.java:431)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Because I am calling the UserRecommend
class from another CustomAdapter class, I believe it is losing the activity.
It is crashing on this specific line:
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
This is my code for where I call this function:
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
holder.recommendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UsersList usersList = new UsersList();
UsersList.UsersRecommend usersRecommend = usersList.new UsersRecommend(usersList);
usersRecommend.execute();
}
});
}