您好我正在构建一个登录应用程序,其中将使用JSON PHP和MYSQL显示用户的详细信息但是只要配置文件显示它就会崩溃我做错了什么?奇怪的是,这是几天前的工作
logcat报告
10-05 18:55:34.088: E/AndroidRuntime(3173): FATAL EXCEPTION: main
10-05 18:55:34.088: E/AndroidRuntime(3173): java.lang.NullPointerException
10-05 18:55:34.088: E/AndroidRuntime(3173): at com.example.itmaproject.EditContactsActivity$GetProductDetails$1.run(EditContactsActivity.java:144)
10-05 18:55:34.088: E/AndroidRuntime(3173): at android.os.Handler.handleCallback(Handler.java:725)
10-05 18:55:34.088: E/AndroidRuntime(3173): at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 18:55:34.088: E/AndroidRuntime(3173): at android.os.Looper.loop(Looper.java:137)
10-05 18:55:34.088: E/AndroidRuntime(3173): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-05 18:55:34.088: E/AndroidRuntime(3173): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 18:55:34.088: E/AndroidRuntime(3173): at java.lang.reflect.Method.invoke(Method.java:511)
10-05 18:55:34.088: E/AndroidRuntime(3173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-05 18:55:34.088: E/AndroidRuntime(3173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-05 18:55:34.088: E/AndroidRuntime(3173): at dalvik.system.NativeStart.main(Native Method)
my codepackage com.example.itmaproject; 公共类EditContactsActivity扩展了Activity {
TextView txtName;
TextView txtNumber;
TextView txtAbout;
TextView txtDate;
Button btnSave;
Button btnDelete;
TextView txtDepart;
TextView fullname;
TextView txtUser;
Button btnLogout;
String username;
SessionManager session;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_contact_details = "http://10.0.3.2/sunshine-ems/login/get_contact_details.php";
// url to update product
private static final String url_update_contact = "http://10.0.3.2/sunshine-ems/login/get_contact_details.php";
// url to delete product
private static final String url_delete_contact = "http://10.0.3.2/sunshine-ems/delete_contact.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CONTACT = "contacts";
private static final String TAG_USERNAME = "username";
private static final String TAG_FIRSTNAME = "firstname";
private static final String TAG_DEPART = "department";
private static final String TAG_LASTNAME = "lastname";
private static final String TAG_ID = "user_id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_contact);
btnLogout = (Button) findViewById(R.id.btnLogout);
// save button
// getting product details from intent
//Intent i = getIntent();
// getting product id (pid) from intent
//pid = i.getStringExtra(TAG_USERNAME);
session = new SessionManager(getApplicationContext());
username = session.getUsername();
// Getting complete product details in background thread
new GetProductDetails().execute();
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching All products Activity
session.logoutUser();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
/**
* Background Async Task to Get complete product details
* */
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditContactsActivity.this);
pDialog.setMessage("Loading contact 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 {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_contact_details, "GET", params);
// check your log for json response
Log.d("Single Contact Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray contactObj = json.getJSONArray(TAG_CONTACT); // JSON Array
// get first product object from JSON Array
JSONObject contact = contactObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.inputName);
txtDepart = (TextView) findViewById(R.id.department);
//txtAbout = (TextView) findViewById(R.id.inputDesc);
txtUser = (TextView) findViewById(R.id.inputPrice);
// display product data in EditText
String fullname = contact.getString(TAG_LASTNAME)+' '+ contact.getString(TAG_FIRSTNAME);
txtName.setText(fullname);
txtDepart.setText(contact.getString(TAG_DEPART));
//txtAbout.setText(contact.getString(TAG_LASTNAME));
txtUser.setText(contact.getString(TAG_ID));
Log.v("blahhhhh", "blah hhhhblah");
}else{
// product with pid not found
Log.v("blah", "blah blah");
}
} 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();
}
}
/**
* Background Async Task to Save product Details
* */
class SaveProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditContactsActivity.this);
pDialog.setMessage("Saving contact ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Saving product
* */
protected String doInBackground(String... args) {
// getting updated data from EditTexts
String name = txtName.getText().toString();
//String number = txtNumber.getText().toString();
//String about = txtAbout.getText().toString();
String user = txtUser.getText().toString();
String depart = txtDepart.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_USERNAME, username));
params.add(new BasicNameValuePair(TAG_FIRSTNAME, name));
params.add(new BasicNameValuePair(TAG_DEPART, depart));
//params.add(new BasicNameValuePair(TAG_LASTNAME, about));
params.add(new BasicNameValuePair(TAG_ID, user));
// sending modified data through http request
// Notice that update product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_update_contact,
"POST", params);
// check json success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully updated
Intent i = getIntent();
// send result code 100 to notify about product update
setResult(100, i);
finish();
} else {
// failed to update product
}
} 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 product uupdated
pDialog.dismiss();
}
}
/*****************************************************************
* Background Async Task to Delete Product
* */
class DeleteProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditContactsActivity.this);
pDialog.setMessage("Deleting Contact...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Deleting product
* */
protected String doInBackground(String... args) {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
url_delete_contact, "POST", params);
// check your log for json response
Log.d("Delete Contact", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// product successfully deleted
// notify previous activity by sending code 100
Intent i = getIntent();
// send result code 100 to notify about product deletion
setResult(100, i);
finish();
}
} 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 product deleted
pDialog.dismiss();
}
}
}
第141行
Log.d("Single Contact Details", json.toString());
答案 0 :(得分:0)
请检查您的网址JSON可能因为它而无效