我打算從我的網站獲取數據,然後將該信息放入微調器中。我用一些教程,我想出了這個代碼Android系統服務不可用
package com.thenewboston.christian;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class hospitalhttp extends Activity{
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private static String url_all_products = "http://www.photosbychristian.com/ems/hospitals.php";
//private static final String TAG_HID = "hid";
private static final String TAG_HOSPITAL = "hospital";
JSONArray hosps = null;
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnertest);
new LoadAllProducts().execute();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(hospitalhttp.this);
pDialog.setMessage("Loading hospitals. 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>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
try {
// products found
// Getting Array of Products
hosps = json.getJSONArray("PA");
// looping through All Products
for (int i = 0; i < hosps.length(); i++) {
JSONObject c = hosps.getJSONObject(i);
// Storing each json item in variable
//String id = c.getString(TAG_HID);
String name = c.getString(TAG_HOSPITAL);
adapter.add(name);
}
} 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
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
*/
Spinner state = (Spinner) findViewById(R.id.spinner1);
state.setAdapter(adapter);
}
});
}
}
}
我嘗試運行我的應用程序和我得到這個錯誤
02-22 22:29:48.131: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thenewboston.christian/com.thenewboston.christian.hospitalhttp}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
所以我把所有的這個OnCreate中bolck和我後仍然得到同樣的錯誤
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
private static String url_all_products = "http://www.photosbychristian.com/ems/hospitals.php";
//private static final String TAG_HID = "hid";
private static final String TAG_HOSPITAL = "hospital";
JSONArray hosps = null;
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
,所以我不知道怎麼知道的任何幫助,將不勝感激
後整個堆棧跟蹤 – 2013-02-22 22:46:39