该应用的目标是:
当我尝试将HashMap添加到ListView时,我收到了错误。
这是代码:
package com.androidopentutorials.imageslideshow.fragment;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.widget.ListView;
import com.androidbegin.jsonparsetutorial.ListViewAdapter;
import com.androidbegin.jsonparsetutorial.R;
import com.androidbegin.jsonparsetutorial.Url;
import com.androidopentutorials.imageslideshow.bean.Product;
import com.androidopentutorials.imageslideshow.json.GetJSONObject;
import com.androidopentutorials.imageslideshow.utils.PageIndicator;
@SuppressLint("ValidFragment")
public class InfoRoom
extends FragmentActivity {
public static final String ARG_ITEM_ID = "home_fragment";
ArrayList<HashMap<String, String>> arraylist;
ListViewAdapter adapter;
ListView listview1;
PageIndicator mIndicator6;
ProgressDialog mProgressDialog;
AlertDialog alertDialog;
RequestImgTask task;
String message;
JSONObject jsonObject;
private Handler handler;
public String id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.inforooms);
// Execute DownloadJSON AsyncTask
//sort="NO_SORT";
new RequestImgTask(this).execute();
listview1 = (ListView) findViewById(R.id.listview1);
}
@Override
public void onPause() {
if (task != null) {
task.cancel(true);
}
if (handler != null) {
//Remove callback
// handler.removeCallbacks(animateViewPager);
}
super.onPause();
}
public void showAlertDialog(String message, final boolean finish) {
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage(message);
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (finish) {
finish();
}
}
});
alertDialog.show();
}
private class RequestImgTask
extends AsyncTask<Void, Void, Void> {
private final WeakReference<Activity> activityWeakRef;
Throwable error;
private Url url1;
private JSONObject jsonobject;
public RequestImgTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
url1 = new Url(pref.getString("datedepart", ""), pref.getString("dateretour", ""), null, null, pref.getString("adult1", ""), pref.getString("adult2", ""), pref.getString("adult3", ""), null, null, pref.getString("ageenfant1room1", ""), pref.getString("ageenfant2room1", ""), pref.getString("ageenfant3room1", ""), pref.getString("ageenfant4room1", ""), pref.getString("ageenfant1room2", ""), pref.getString("ageenfant2room2", ""), pref.getString("ageenfant3room2", ""), pref.getString("ageenfant4room2", ""), pref.getString("ageenfant1room3", ""), pref.getString("ageenfant2room3", ""), pref.getString("ageenfant3room3", ""), pref.getString("ageenfant4room3", ""), null, null, null, null, null, pref.getString("enfant1", ""), pref.getString("enfant2", ""), pref.getString("enfant3", ""), id = pref.getString("id", null));
System.out.println(url1.get_rooms());
jsonObject = getJsonObject(url1.get_rooms());
System.out.println(jsonobject);
try {
JSONArray json = jsonObject.getJSONObject("HotelRoomAvailabilityResponse").getJSONArray("HotelRoomResponse");
System.out.println(json);
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
// Storing each json item values in variable
String id = c.getString("roomTypeCode");
String name = c.getString("rateDescription");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id", id);
map.put("name", name);
// adding HashList to ArrayList
arraylist.add(map);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
public JSONObject getJsonObject(String url) {
JSONObject jsonObject = null;
try {
System.out.println(url);
jsonObject = GetJSONObject.getJSONObject(url);
} catch (Exception e) {
}
System.out.println(jsonObject);
return jsonObject;
}
@Override
protected void onPostExecute(Void args) {
if (activityWeakRef != null && !activityWeakRef.get().isFinishing()) {
if (error != null && error instanceof IOException) {
message = getResources().getString(R.string.time_out);
showAlertDialog(message, true);
} else if (error != null) {
message = getResources().getString(R.string.error_occured);
showAlertDialog(message, true);
} else {
//products = result;
if (arraylist != null) {
System.out.println("emptyy");
adapter = new ListViewAdapter(getApplicationContext(), arraylist);
listview1.setAdapter(adapter);
}
}
}
}
}
}
答案 0 :(得分:0)
您尝试将地图添加到未初始化的ArrayList
。
请确保首先初始化ArrayList。
您可以将此作为全局声明:
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
或者你确定在添加项目之前检查arrayList是否为!= null。
if(arraylist == null) {
arraylist = new ArrayList<HashMap<String, String>>();
}
arraylist.add(map);