Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm new to Android programming and I can't pinpoint which triggers the error. Can someone please explain to me the error and how to get rid of it?
I have this errors:
09-16 10:09:05.529: E/AndroidRuntime(26221): FATAL EXCEPTION: AsyncTask #1
09-16 10:09:05.529: E/AndroidRuntime(26221): java.lang.RuntimeException: An error occured while executing doInBackground()
09-16 10:09:05.529: E/AndroidRuntime(26221): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-16 10:09:05.529: E/AndroidRuntime(26221): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.lang.Thread.run(Thread.java:841)
09-16 10:09:05.529: E/AndroidRuntime(26221): Caused by: java.lang.NullPointerException
09-16 10:09:05.529: E/AndroidRuntime(26221): at com.databaseaar.Login_new$AttemptLogin.doInBackground(Login_new.java:82)
09-16 10:09:05.529: E/AndroidRuntime(26221): at com.databaseaar.Login_new$AttemptLogin.doInBackground(Login_new.java:1)
09-16 10:09:05.529: E/AndroidRuntime(26221): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-16 10:09:05.529: E/AndroidRuntime(26221): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-16 10:09:05.529: E/AndroidRuntime(26221): ... 4 more
09-16 10:09:06.459: E/WindowManager(26221): Activity com.databaseaar.Login_new has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42c25fb0 V.E..... R......D 0,0-684,192} that was originally added here
09-16 10:09:06.459: E/WindowManager(26221): android.view.WindowLeaked: Activity com.databaseaar.Login_new has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42c25fb0 V.E..... R......D 0,0-684,192} that was originally added here
09-16 10:09:06.459: E/WindowManager(26221): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:452)
09-16 10:09:06.459: E/WindowManager(26221): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
09-16 10:09:06.459: E/WindowManager(26221): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
09-16 10:09:06.459: E/WindowManager(26221): at android.app.Dialog.show(Dialog.java:287)
09-16 10:09:06.459: E/WindowManager(26221): at com.databaseaar.Login_new$AttemptLogin.onPreExecute(Login_new.java:66)
09-16 10:09:06.459: E/WindowManager(26221): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
09-16 10:09:06.459: E/WindowManager(26221): at android.os.AsyncTask.execute(AsyncTask.java:534)
09-16 10:09:06.459: E/WindowManager(26221): at com.databaseaar.Login_new$1.onClick(Login_new.java:49)
09-16 10:09:06.459: E/WindowManager(26221): at android.view.View.performClick(View.java:4475)
09-16 10:09:06.459: E/WindowManager(26221): at android.view.View$PerformClick.run(View.java:18786)
09-16 10:09:06.459: E/WindowManager(26221): at android.os.Handler.handleCallback(Handler.java:730)
09-16 10:09:06.459: E/WindowManager(26221): at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 10:09:06.459: E/WindowManager(26221): at android.os.Looper.loop(Looper.java:137)
09-16 10:09:06.459: E/WindowManager(26221): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-16 10:09:06.459: E/WindowManager(26221): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 10:09:06.459: E/WindowManager(26221): at java.lang.reflect.Method.invoke(Method.java:525)
09-16 10:09:06.459: E/WindowManager(26221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
09-16 10:09:06.459: E/WindowManager(26221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
09-16 10:09:06.459: E/WindowManager(26221): at dalvik.system.NativeStart.main(Native Method)
And this is my code for the class Login_new and asynctask:
package com.databaseaar;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login_new extends Activity implements OnClickListener{
private EditText user, pass;
private Button bLogin;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://xxxx.com/login_new2.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public static final String KEY_USERNAME ="username";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login2);
user = (EditText)findViewById(R.id.ET_email);
pass = (EditText)findViewById(R.id.ET_password);
bLogin = (Button)findViewById(R.id.btnLogin);
//bLogin.setOnClickListener(Login_new.this);
bLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
default:
break;
class AttemptLogin extends AsyncTask<String, String, String> {
// public static final String KEY_USERNAME = null;
boolean failure = false;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login_new.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
@Override
protected String doInBackground(String... args) {
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
// success tag for json
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
finish();
startActivity(ii);
return json.getString(TAG_MESSAGE);
}else {
return json.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
return null;
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null){
Toast.makeText(Login_new.this, message, Toast.LENGTH_LONG).show();
PHP code:
$con=mysqli_connect("xxxx","xxxx","xxxx","xxxx");
$password=$_POST["password"];
$username=$_POST["username"];
if (!empty($_POST)) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$response["success"] = 0;
$response["message"] = "One or both of the fields are empty .";
$query = " SELECT * FROM login WHERE username = '$username'and password='$password'";
$sql1=mysql_query($query);
$row = mysql_fetch_array($sql1);
if (!empty($row)) {
$response["success"] = 1;
$response["message"] = "You have been sucessfully login";
die(json_encode($response));
}else{
$response["success"] = 0;
$response["message"] = "invalid username or password ";
die(json_encode($response));
else{
$response["success"] = 0;
$response["message"] = " One or both of the fields are empty ";
die(json_encode($response));
mysql_close();
My JSON httpRequest
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
// Making HTTP Request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHTTPClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer error", "Error converting result" + e.toString());
// try parse the string to a JSON object
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
return jsonObj;
New error log cat:
09-08 08:30:14.854: E/Response(2297): <br />
09-08 08:30:14.854: E/Response(2297): <b>Deprecated</b>: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>/home/u747670603/public_html/login_new2.php</b> on line <b>14</b><br />
09-08 08:30:14.854: E/Response(2297): <br />
09-08 08:30:14.854: E/Response(2297): <b>Warning</b>: mysql_query(): Access denied for user 'u747670603'@'10.2.1.25' (using password: NO) in <b>/home/u747670603/public_html/login_new2.php</b> on line <b>14</b><br />
09-08 08:30:14.854: E/Response(2297): <br />
09-08 08:30:14.854: E/Response(2297): <b>Warning</b>: mysql_query(): A link to the server could not be established in <b>/home/u747670603/public_html/login_new2.php</b> on line <b>14</b><br />
09-08 08:30:14.854: E/Response(2297): <br />
09-08 08:30:14.854: E/Response(2297): <b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>/home/u747670603/public_html/login_new2.php</b> on line <b>15</b><br />
09-08 08:30:14.854: E/Response(2297): {"success":0,"message":"invalid username or password "}
As the Crash dump suggests, It's the problem with the progressDialog which initiates in the onPreExecute() method. But in your doInBackground() method starts an activity on some condition without dismissing the progressDialog. This leads to Window Leakage shown in the crash dump.
doInBackground() should be used to do the networking or other harder tasks. UI related API's should not be called inside this method as this method runs on different thread(Not on Main GUI thread). So I suggest you to move the code that starts the new activity to onPostExecute() method and the progressDialog should be dismissed before launching the new activity.
Here is a sample code.
class AttemptLogin extends AsyncTask<String, String, String> {
// public static final String KEY_USERNAME = null;
boolean failure = false;
String username, password;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login_new.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
username = user.getText().toString();
password = pass.getText().toString();
@Override
protected String doInBackground(String... args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
return jsonParser.makeHttpRequest(LOGIN_URL, "POST", params).toString();
} catch (Exception e) {
return null;
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null) {
Toast.makeText(Login_new.this, message, Toast.LENGTH_LONG).show();
try {
JSONObject object = new JSONObject(message);
// success tag for json
int success = object.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
startActivity(ii);
finish();
} catch (JSONException e) {
e.printStackTrace();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
is.close();
json = sb.toString();
* This line will print the server response.
* Check this response and make changes to the PHP&MySQL accordingly.
* Post this response for further help
Log.e ("Response", json);
} catch (Exception e) {
Log.e("Buffer error", "Error converting result" + e.toString());
Here is a sample PHP login script (Add the $username, $password assignment statement before using them)
login.php
require_once('Database.php');
$db = new Database();
$stmt = $db->prepare("SELECT * FROM login where username = :username AND password = :password");
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);
$stmt->execute();
if ($stmt->rowCount() == 1) {
$result['status'] = 1;
$result['msg'] = "Successfully logged in";
} else {
$result['status'] = 0;
$result['msg'] = "Invalid login credentials";
echo json_encode($result);
Create a new PHP class file Database.php add the below code in it.
class Database extends PDO {
function __construct() {
parent::__construct('mysql:host=localhost;dbname=dbName', 'username', 'password');
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Don't forget to change the host name, DB name, username and password in the above class constructor.
–
–
The reason is json.toString() sometimes is null based on your server code, so you're getting NullPointerException.
remove that line
Log.d("Login attempt", json.toString());
Log.d("Successfully Login!", json.toString());
–
why finish() before start activity?
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
finish();
startActivity(ii);
your Code should be
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
startActivity(ii);
and in post execute do finish();
do your code like this.
public class Login_new extends Activity implements OnClickListener{
private EditText user, pass;
private Button bLogin;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://xxxx.com/login_new2.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
public static final String KEY_USERNAME ="username";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login2);
user = (EditText)findViewById(R.id.ET_email);
pass = (EditText)findViewById(R.id.ET_password);
bLogin = (Button)findViewById(R.id.btnLogin);
//bLogin.setOnClickListener(Login_new.this);
bLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
default:
break;
class AttemptLogin extends AsyncTask<String, String, Boolean> {
// public static final String KEY_USERNAME = null;
boolean failure = false;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login_new.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
@Override
protected String doInBackground(String... args) {
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Log.d("Login attempt", json.toString());
// success tag for json
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
startActivity(ii);
failure = false;
return json.getString(TAG_MESSAGE);
}else {
return json.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
return failur;
protected void onPostExecute(boolean fail) {
pDialog.dismiss();
if (message == true){
finish();
Toast.makeText(Login_new.this, message, Toast.LENGTH_LONG).show();
Put the below code in onPostExecute()
rather than doInBackground()
Intent ii = new Intent(Login_new.this,MainActivity.class);
ii.putExtra(KEY_USERNAME, username);
finish();
startActivity(ii);
The reason is you are trying to finish the current activity
and try to start a new activity
while the progressdialog
is still showing that's why there is leaked window error showing there.And also the doInBackground()
method is expecting String
in return.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.