2013-02-22 78 views
0

我打算從我的網站獲取數據,然後將該信息放入微調器中。我用一些教程,我想出了這個代碼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); 

,所以我不知道怎麼知道的任何幫助,將不勝感激

+3

後整個堆棧跟蹤 – 2013-02-22 22:46:39

回答

3

移動代碼不會有幫助。 Java是一種範圍內的語言,如果在某個方法之後或之前放置了某些內容,則無關緊要。它仍然在相同的範圍內。

的問題可能是由該行提出:

ArrayAdapter <String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); 

你想在對象加載佈局範圍的初始化。這種代碼甚至在構造函數之前運行。您必須將值分配移動到onCreate方法是這樣的:

ArrayAdapter <String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.spinnertest); 

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); 

    new LoadAllProducts().execute(); 
} 
+0

這是我需要感謝你 – cmb 2013-02-22 23:03:44

0

所以我把所有的這個OnCreate中bolck後,我仍然得到同樣的錯誤

該代碼不能是方法裏面,因爲你正在申報的靜態值。因此,該代碼必須駐留在任何方法之外,這意味着它將在執行任何方法之前執行,因此將在onCreate()之前執行。

東西在裏面 - 可能是你JSONParser - 正在試圖做一些事情,不能到完成後super.onCreate()onCreate()方法已經執行。您的Java堆棧跟蹤將明確告訴您哪些行(如果有)是您的難題的來源。然後,您需要在調用super.onCreate()後執行該代碼,例如將其置於onCreate()方法的末尾。

+0

豈不'adapter'宣言也惹的禍?這實際上是通過了一個上下文。 – 2013-02-22 22:54:45

+0

@ A - C:可能。但是我不會期望它在構造函數中嘗試使用類似'LayoutInflater'的東西,所以這就是我爲什麼猜測'JSONParser'。 – CommonsWare 2013-02-22 23:21:10

相關問題