我很想从我的网站获取数据,然后将这些信息放入微调器中。我已经使用了一些教程,我想出了这段代码
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 );
所以我不知道该怎么做知道任何帮助将不胜感激