我有一个应用程序,必须显示来自localhost服务器的studentdata,但我收到错误:An error occured while executing doInBackground, which is caused by a NullPointerException
。
01-26 12:25:42.924 1193-1216/com.ipmedt4.challengeweek_v2 E/Buffer Error﹕ Error converting resultjava.lang.NullPointerException: lock == null
01-26 12:25:42.924 1193-1216/com.ipmedt4.challengeweek_v2 E/JSON Parser﹕ Error parsing dataorg.json.JSONException: End of input at character 0 of
01-26 12:25:42.924 1193-1216/com.ipmedt4.challengeweek_v2 W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xa4ba0648)
01-26 12:25:42.928 1193-1216/com.ipmedt4.challengeweek_v2 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.ipmedt4.challengeweek_v2.OverzichtStudenten$LaadAlleStudenten.doInBackground(OverzichtStudenten.java:105)
at com.ipmedt4.challengeweek_v2.OverzichtStudenten$LaadAlleStudenten.doInBackground(OverzichtStudenten.java:92)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
01-26 12:25:43.084 1193-1193/com.ipmedt4.challengeweek_v2 W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-26 12:25:43.484 1193-1193/com.ipmedt4.challengeweek_v2 E/WindowManager﹕ Activity com.ipmedt4.challengeweek_v2.OverzichtStudenten has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52867d60 V.E..... R......D 0,0-552,96} that was originally added here
android.view.WindowLeaked: Activity com.ipmedt4.challengeweek_v2.OverzichtStudenten has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52867d60 V.E..... R......D 0,0-552,96} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:281)
at com.ipmedt4.challengeweek_v2.OverzichtStudenten$LaadAlleStudenten.onPreExecute(OverzichtStudenten.java:99)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.ipmedt4.challengeweek_v2.OverzichtStudenten.onCreate(OverzichtStudenten.java:67)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我的班级显示学生数据:
package com.ipmedt4.challengeweek_v2;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class OverzichtStudenten extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> studentenlijst;
private static String url_alle_studenten = "http://10.0.2.2/android_connect/getAlleStudenten.php";
private static final String TAG_SUCCESS = "succes";
private static final String TAG_STUDENTEN = "Studenten";
private static final String TAG_ID = "ID";
private static final String TAG_NAME = "Naam";
private static final String TAG_STUDENTNUMMER = "Studentnummer";
private static final String TAG_KLAS = "Klas";
private static final String TAG_GROEP = "Groep";
JSONArray studenten = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
studentenlijst = new ArrayList<HashMap<String, String>>();
new LaadAlleStudenten().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.ID)).getText().toString();
Intent in = new Intent(getApplicationContext(), Beoordelingscherm.class);
in.putExtra(TAG_ID, ID);
startActivityForResult(in, 100);
}
});
}
@Override
protected void onActivityResult (int requestCode, int resultcode, Intent data) {
super.onActivityResult(requestCode, resultcode, data);
if (resultcode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LaadAlleStudenten extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(OverzichtStudenten.this);
pDialog.setMessage("Lijst van studenten is aan het laden. Geduld a.u.b.");
pDialog.setIndeterminate(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_alle_studenten, "GET", params);
Log.d("Alle studenten: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
studenten = json.getJSONArray(TAG_STUDENTEN);
for (int i = 0; i < studenten.length(); i++) {
JSONObject c = studenten.getJSONObject(i);
String ID = c.getString(TAG_ID);
String Naam = c.getString(TAG_NAME);
String Studentnummer = c.getString(TAG_STUDENTNUMMER);
String Klas = c.getString(TAG_KLAS);
String Groep = c.getString(TAG_GROEP);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, ID);
map.put(TAG_NAME, Naam);
map.put(TAG_STUDENTNUMMER, Studentnummer);
map.put(TAG_KLAS, Klas);
map.put(TAG_GROEP, Groep);
studentenlijst.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(), Beginscherm.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter = new SimpleAdapter(OverzichtStudenten.this, studentenlijst, R.layout.student_info,
new String[]{TAG_ID, TAG_NAME, TAG_STUDENTNUMMER, TAG_KLAS, TAG_GROEP},
new int[]{R.id.ID, R.id.Naam, R.id.Studentnummer, R.id.Klas, R.id.Groep});
setListAdapter(adapter);
}
});
}
}}
我的JSONParser课程:
package com.ipmedt4.challengeweek_v2;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Created by Charlie on 23-1-2015.
*/
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser(){
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
try {
if (method == "POST") {
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 {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
return jObj;
}}
我希望你能帮助我。该应用程序显示对话框屏幕,但然后崩溃。谢谢你的帮助。