我已经开发了连接到服务器并接收 JSON 响应的应用程序。直到它工作正常。但是它在 ArrayList 中放入 Hashmap 和 HashMap 以显示数据。它在将 Hashmap 放入 ArrayList 时出错。我的 LogCat 是
06-03 16:23:10.540: E/AndroidRuntime(15457): FATAL EXCEPTION: AsyncTask #1
06-03 16:23:10.540: E/AndroidRuntime(15457): java.lang.RuntimeException: An error occured while executing doInBackground()
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.lang.Thread.run(Thread.java:856)
06-03 16:23:10.540: E/AndroidRuntime(15457): Caused by: java.lang.NullPointerException
06-03 16:23:10.540: E/AndroidRuntime(15457): at com.example.project.MainActivity$LoadAllProducts.doInBackground(MainActivity.java:498)
06-03 16:23:10.540: E/AndroidRuntime(15457): at com.example.project.MainActivity$LoadAllProducts.doInBackground(MainActivity.java:1)
06-03 16:23:10.540: E/AndroidRuntime(15457): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-03 16:23:10.540: E/AndroidRuntime(15457): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-03 16:23:10.540: E/AndroidRuntime(15457): ... 5 more
我的课是
public class asasa extends Activity {
//ListView listView;
Intent intent;
public int currentimageindex=0;
private ProgressDialog pDialog;
Timer timer;
TimerTask task;
ImageView slidingimage;
ProgressDialog progressDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
static final String URL = "http://172.20.55.175/android_connect/get_all_products.php";
private static final String TAG_SUCCESS = "success";
private TextView mStatusView;
private EditText searchEditText;
private static final String TAG_PRODUCTS = "products";
private static final String TAG_NAME = "name";
private static final String TAG_Description = "description";
private static final String TAG_URL = "url";
private static final String TAG_Price = "price";
ListView list;
LazyAdapter adapter;
// flag for Internet connection status
Boolean isInternetPresent = false;
// products JSONArray
JSONArray products = null;
// Connection detector class
ConnectionDetector cd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
LoadAllProducts il = new LoadAllProducts();
il.execute(URL);
}
else
{
// Internet connection is not present
// Ask user to connect to Internet
Toast.makeText(asasa.this, "No Internet Connection You don't have internet connection.", Toast.LENGTH_LONG).show();
}
}
public void longToast(CharSequence message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(asasa.TAG_NAME));
artist.setText(song.get(asasa.TAG_Description));
duration.setText(song.get(asasa.TAG_Price));
imageLoader.DisplayImage(song.get(asasa.TAG_URL), thumb_image);
return vi;
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
// creating new HashMap
ArrayList<HashMap<String, String>> productsList;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(asasa.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
HashMap<String, String> map = new HashMap<String, String>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(URL, "GET", params);
// Check your log cat for JSON reponse
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length()-1; i++) {
JSONObject c = products.getJSONObject(i);
String name = c.getString(TAG_NAME);
String description = c.getString(TAG_Description);
String URl = c.getString(TAG_URL);
String price = c.getString(TAG_Price);
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_Description, description);
map.put(TAG_URL, URl);
map.put(TAG_Price, price);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(asasa.this, productsList);
list.setAdapter(adapter);
}
}
public void onPause()
{
super.onPause();
if (progressDialog.isShowing()) {
progressDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
第 498 行是
productsList.add(map);
在受保护的字符串中 doInBackground(String... args)
我无法弄清楚错误...为什么会发生。它成功返回 json 响应但可以放入 hashmap