当我想从数据库中检索记录的详细信息时出现问题,当我运行项目时它告诉我项目已停止工作。错误在这一行JSONObject json = jParser.makeHttpRequest(url_Edu_details," GET",params);
package com.example.dell.adegma;
import java.util.ArrayList;
import java.util.HashMap;
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.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GetEduDetails extends GetRecords {
TextView txtName;
String ID;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private static final String url_Edu_details= "http://10.10.88.105/app_connect/get_school_details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_EDUCATION = "education";
private static final String TAG_ID = "ID";
private static final String TAG_SNAME = "SName";
private static final String TAG_TYPE = "Type";
private static final String TAG_GENDER = "Gender";
private static final String TAG_GRADES = "Grades";
private static final String TAG_LOCATION = "Location";
private static final String TAG_TELEPHONE = "Telephone";
private static final String TAG_CURRICULUM = "Curriculum";
private static final String TAG_EMAIL = "Email";
private static final String TAG_FEES = "Fees";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_get_records);
Intent i = getIntent();
ID = i.getStringExtra(ID);
new loadEduDetails().execute();
}
/**
* Background Async Task to Get complete school details
* */
class loadEduDetails extends AsyncTask<String, String, String> {
/* Before starting background thread Show Progress Dialog*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(GetEduDetails.this);
pDialog.setMessage("Loading school details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting school 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
HashMap<String, String> params = new HashMap<>();
params.put(TAG_ID,ID);
// getting school details by making HTTP request
// Note that school details url will use GET request
JSONObject json = jParser.makeHttpRequest(url_Edu_details, "GET", params);
// check your log for json response
Log.d("Single school Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received school details
JSONArray educationObj = json.getJSONArray(TAG_EDUCATION); // JSON Array
// get first school object from JSON Array
JSONObject education = educationObj.getJSONObject(0);
// school with this id found
// Edit Text
txtName = (TextView) findViewById(R.id.SchoolName);
// display data in TextView
txtName.setText(education.getString(TAG_SNAME));
}else{
// school with id 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();
}
}
}
这是logcat: -
03-31 15:18:17.144 6012-6012/com.example.dell.adegma D/AndroidRuntime: Shutting down VM
03-31 15:18:17.146 6012-6012/com.example.dell.adegma E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dell.adegma, PID: 6012
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.example.dell.adegma.JSONParser.makeHttpRequest(JSONParser.java:38)
at com.example.dell.adegma.GetEduDetails$loadEduDetails$1.run(GetEduDetails.java:107)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5651)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)