This is the wireframe of how my class is built:
public class HelloWorldActivity extends Activity
{
private ProgressDialog progressdialog;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private class AuthenticateUserTask extends AsyncTask<Void,Void,String>
{
protected String doInBackground(Void... params)
{
//do something
}
protected void onPostExecute(String result)
{
if (result.equals("success"))
//do something
else
{
progressdialog.dismiss(); //this throws a null pointer exception
/*
When above line is commented to allow execution of app, Toast never shows on screen
however no errors are recorded in the log
*/
Toast.makeText(getApplicationContext(),"Login failed",Toast.LENGTH_SHORT);
}
}
}
public void login(View v)
{
progressdialog.show(HelloWorldActivity.this,"","Logging in...",true);
new AuthenticateUserTask().execute();
}
}
I’m sorry if it seems I’m asking two questions in one thread, but I suspect the solution is similar for both.
Within onPostExecute:
1. If login fails and I do progressdialog.dismiss(), a null pointer exception is thrown and application crashes.
2. If I remove the ProgressDialog and attempt to show a Toast, nothing show up on the screen, however no exception or error is recorded either.
EDIT
I found the solution to my first problem in another thread here.
I had to display the ProgressDialog in an onPreExecute method.
To get the
ProgressDialogto show, override the following method in yourAsyncTask:Note: you need to remove this line from your
login()method:The reason your
Toastis not showing is that you forgot to call.show():