NullpointerException dans l'activity
bonjour,
Il y'a quelque temps je commence à m'intéresser a la programmation Android et je vous m’entraîner en créant une application qui nécessite une authentification ,j'ai trouvé se super tuto http://www.androidhive.info/2012/05/...ith-php-mysql/ et j'aimerai l'appliquer mais j'ai eu un probleme de compilation que j'ai du mal a en detecter la source.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
11-23 17:36:18.964: E/AndroidRuntime(2265): FATAL EXCEPTION: main
11-23 17:36:18.964: E/AndroidRuntime(2265): java.lang.NullPointerException
11-23 17:36:18.964: E/AndroidRuntime(2265): at com.ta3alam.activity.MainActivity$GetUser$1.run(MainActivity.java:126)
11-23 17:36:18.964: E/AndroidRuntime(2265): at android.os.Handler.handleCallback(Handler.java:615)
11-23 17:36:18.964: E/AndroidRuntime(2265): at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 17:36:18.964: E/AndroidRuntime(2265): at android.os.Looper.loop(Looper.java:137)
11-23 17:36:18.964: E/AndroidRuntime(2265): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 17:36:18.964: E/AndroidRuntime(2265): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 17:36:18.964: E/AndroidRuntime(2265): at java.lang.reflect.Method.invoke(Method.java:511)
11-23 17:36:18.964: E/AndroidRuntime(2265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-23 17:36:18.964: E/AndroidRuntime(2265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 17:36:18.964: E/AndroidRuntime(2265): at dalvik.system.NativeStart.main(Native Method) |
voila l'activité en question :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| package com.ta3alam.activity;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
ProgressDialog pDialog;
JSONParser jsonParser;
String login ="par Defaut";
String password ="par defaut";
String loginSaisi;
String passwordSaisi;
EditText inputLogin = null;
EditText inputPassword = null;
Button btnConnexion = null;
Button btnInscription = null;
// url to create new product
private static String url_get_user_details = "http://192.168.1.8/ta3alam_conectitivty/get_user_details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_USER = "utilisateur";
private static final String TAG_LOGIN="login";
private static final String TAG_PASSWORD="password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authentification);
inputLogin =(EditText)findViewById(R.id.editTextLogin);
inputPassword =(EditText)findViewById(R.id.editTextPassword);
btnConnexion = (Button)findViewById(R.id.buttonConnection);
btnConnexion.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new GetUser().execute();
}
});
btnInscription =(Button)findViewById(R.id.btnInscription);
btnInscription.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),NewUserActivity.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class GetUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
loginSaisi = inputLogin.getText().toString();
passwordSaisi = inputPassword.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("login", loginSaisi));
// getting product details by making HTTP request
// Note that product details url will use GET request
//
JSONObject json = jsonParser.makeHttpRequest(
url_get_user_details, "GET", params);//le debogage bloque ici
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_USER); // JSON Array
// get first product object from JSON Array
JSONObject user = productObj.getJSONObject(0);
login = user.getString(TAG_LOGIN);
password = user.getString(TAG_PASSWORD);
if(testAuthentification(login, password, loginSaisi, passwordSaisi))
{
Intent i = new Intent(getApplicationContext(),Bienvenu.class);
startActivity(i);
}
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
private boolean testAuthentification(final String login1 ,final String password1,final String login2,final String password2)
{
if(login1.equals(login2) && password1.equals(password2))
{
return true;
}else
{
return false;
}
}
} |
dans cette activité je veux juste premettre a l'utilisateur de saisir un login et un mots de passe et les comparer avec le login et le mots de passe dans la base de donnée
le deboguage bloque dans cette ligne JSONObject json = jsonParser.makeHttpRequest(
url_get_user_details, "GET", params);//le debogage bloque ici
j'ai testé le code php il marche mais je le poste quand meme juste pour vous mettre dans le contexte
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| <?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["login"])) {
$login = $_GET['login'];
// get a product from products table
$result = mysql_query("SELECT *FROM utilisateur WHERE login = '$login'");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$utilisateur = array();
$utilisateur["id_utlisateur"] = $result["id_utlisateur"];
$utilisateur["prenom"] = $result["prenom"];
$utilisateur["profession"] = $result["profession"];
$utilisateur["adress"] = $result["adress"];
$utilisateur["tel"] = $result["tel"];
$utilisateur["email"] = $result["email"];
$utilisateur["login"] = $result["login"];
$utilisateur["password"] = $result["password"];
$utilisateur["role"] = $result["role"];
// success
$response["success"] = 1;
// user node
$response["utilisateur"] = array();
array_push($response["utilisateur"], $utilisateur);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found 1";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found 2";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?> |