私は自分のウェブサイトからデータを取得し、その情報をスピナーに入れるのに苦労しています。いくつかのチュートリアルを使用して、このコードを思いつきました
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 );
だから私は何をすべきかわからない