我正在尝试从JSON响应生成列表视图但无法执行此操作。 在线获取错误:“list_view.setAdapter(customAdapter);” @onPostExecute
上面一行的NullPointerException ...
完整代码:
public class Screen2 extends AppCompatActivity {
ProgressDialog pDialog;
ArrayList<Information> record_list;
ListView list_view;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
record_list=new ArrayList<Information>();
list_view=(ListView) findViewById(R.id.list_view);
new Test().execute();
}
public class Test extends AsyncTask<String, Void, ArrayList<Information>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Screen2.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected ArrayList<Information> doInBackground(String... params) {
if(record_list.size()>0){record_list.clear();}
String jsonStr = makeServiceCall();
//ssen = new ArrayList<Information>();
try {
//HashMap<String,String> hashMap=new HashMap<>();
JSONArray jsonArray=new JSONArray(jsonStr);
for(int i=0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("label");
String email = jsonObject.getString("email");
Information information = new Information(id, name, email);
System.out.println("id: " + id + "name:" + name + "email" + email);
record_list.add(information);
}
System.out.println(jsonStr);
return record_list;
//return record_list;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<Information> s) {
super.onPostExecute(s);
for(int i=0;i<record_list.size();i++){
System.out.println(record_list.get(i));
}
if(record_list!=null && !record_list.isEmpty()){
customAdapter=new CustomAdapter(Screen2.this,record_list);
list_view.setAdapter(customAdapter);
}
else {
Toast.makeText(Screen2.this, "Array List is Empty", Toast.LENGTH_SHORT).show();
}
//ListAdapter listAdapter1=new ArrayAdapter<String>(Screen2.this,R.layout.custom_row,record_list);
//list_view.setAdapter(listAdapter);
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
}
public String makeServiceCall(){
String response = null;
try {
URL url = new URL("http://192.168.1.127:9000/tasks2");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
System.out.println("MalformedURLException: " + e.getMessage());
} catch (IOException e) {
System.out.println("MalformedURLException: " + e.getMessage());
} catch (Exception e) {
System.out.println("MalformedURLException: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(sb);
return sb.toString();
}
}
}
请浏览堆栈跟踪以获取上述代码... 如果有人解决了这个问题,对我来说非常有帮助。
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.hariprasad.healthpassportnew1.Screen2$Test.onPostExecute(Screen2.java:99)
at com.example.hariprasad.healthpassportnew1.Screen2$Test.onPostExecute(Screen2.java:51)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
也许您的record_list
为空
所以请添加
if (record_list.size > 0) {
list_view.setAdapter(customAdapter);
}
以及在record_list
中添加数据之前清楚
将此代码放在String jsonStr = makeServiceCall();
if (record_list.size > 0) {
record_list.clear();
}