I have doInBackground(). after function I want to change some TextView on MainActivity.
but When I trigger the function on main activity I got NullPointerException on TextView line.
protected String doInBackground(Object... arguments) {
some code..
return result;
}
@Override
protected void onPostExecute(String result) {
if(result!=null)
new MainActivity().setScoreListUpdate(result);
}
MainActivity:
public void setScoreListUpdate(String settings)
{
String[] yeniscore = settings.split("\\|");
if(yeniscore.length > 1)
{
birinci.setText(yeniscore[1]); << NULLPOINTEREXC.
}
}
The reason this gives a
NullPointerExceptionis that you create a new instance ofMainActivityinstead of working on your existing one.What you need to do is pass a reference to your
Activityto theAsyncTaskand then call your method on that reference.So in your
AsyncTask-class you will have a variable:Add a constructor to your
AsyncTask-class:Then in
onPostExecuteyou do: