我正在使用Simpleadapter创建一个Lisview来显示一些数据,但是我收到了这个错误:
java.lang.NullPointerException, at com.example.example.FindPeopleFragment.ListDrwaer(FindPeopleFragment.java:214)
代码
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("Utente");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("Nome");
String number = jsonChildNode.optString("Cognome");
String string = jsonChildNode.optString("Stringa");
String data = jsonChildNode.optString("DataLogin");
String outPut = name + "-" + number + "-" + string + "-" + data ;
Toast.makeText(getActivity(),outPut ,
Toast.LENGTH_SHORT).show();
employeeList.add(createEmployee("Utente", outPut));
}
} catch (JSONException e) {
Toast.makeText(getActivity(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,android.R.layout.simple_list_item_1,
new String[] { "employees" },
new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
最后一行是214行。为什么我收到此错误?
这是logcat:
11-20 10:49:23.287: E/AndroidRuntime(24897): Process: com.example.richapp, PID: 24897
11-20 10:49:23.287: E/AndroidRuntime(24897): java.lang.NullPointerException
11-20 10:49:23.287: E/AndroidRuntime(24897): at com.example.richapp.FindPeopleFragment.ListDrwaer(FindPeopleFragment.java:214)
11-20 10:49:23.287: E/AndroidRuntime(24897): at com.example.richapp.FindPeopleFragment$JsonReadTask.onPostExecute(FindPeopleFragment.java:176)
11-20 10:49:23.287: E/AndroidRuntime(24897): at com.example.richapp.FindPeopleFragment$JsonReadTask.onPostExecute(FindPeopleFragment.java:1)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.os.Handler.dispatchMessage(Handler.java:102)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.os.Looper.loop(Looper.java:157)
11-20 10:49:23.287: E/AndroidRuntime(24897): at android.app.ActivityThread.main(ActivityThread.java:5356)
11-20 10:49:23.287: E/AndroidRuntime(24897): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 10:49:23.287: E/AndroidRuntime(24897): at java.lang.reflect.Method.invoke(Method.java:515)
11-20 10:49:23.287: E/AndroidRuntime(24897): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-20 10:49:23.287: E/AndroidRuntime(24897): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-20 10:49:23.287: E/AndroidRuntime(24897): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你一定有错误, 你没有正确定义ArrayAdapter, 它只有六个构造函数中的两个变量:
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Constructor
在您的代码中,您必须更改分配给new String[] { "employees" }
的构造函数的第三个变量,并将要在每个listview项中使用的TextView的ID放在其位置,以便R.id.yourtextview
和之后,您可以指定您可以在第四个变量中为您提供数据。
可以在您创建的CustomArrayAdapter类中分配其他数据