I am unable to call an activity from async task, i have tried different ways but every time i get a null pointer exception in the log cat.
I have tried this.
@Override
protected void onPostExecute(String result) {
if(signedin==false)
{
Toast.makeText(XMPPClient.context, "authentication failed:", Toast.LENGTH_LONG).show();
}
Intent i = new Intent(getApplicationContext(), ProfileHome.class);
startActivity(i);
}
and this
@Override
protected void onPostExecute(String result) {
XMPPClient xmpp = new XMPPClient();
if(signedin==false)
{
Toast.makeText(XMPPClient.context, "authentication failed:", Toast.LENGTH_LONG).show();
}else
xmpp.startnewactivity();
}
And in XMPPClient.class
public void startnewactivity()
{
Intent i = new Intent(getApplicationContext(), ProfileHome.class);
startActivity(i);
}
How can i call an activity from async task, actually I want to call activity when async task finishes.
I believe the problem is here:
Usually, using
getApplicationContext()
as aContext
is a very bad idea unless you really know what are you doing. Instead, you should use theContext
of theActivity
that you want to start the new from. You can achieve this passing it as a value on.execute()
or even storing theContext
you get in theAsyncTask
's constructor and store it, so you later can use it in youronPostExecute()
method.---- EDIT ----
This would be an example: