2012-09-24 30 views
1

超類成員在我的Android項目有小錯誤,我不能從子的AsyncTask類訪問超類的字符串變量我得到了以下錯誤不能訪問Android的

public class XyzActivity extends ListActivity { 

    // JSON Node names 
       private static final String TAG_CONTACTS = "results"; 
       private static final String TAG_ID = "id"; 
       private static final String TAG_NAME = "name"; 
       private static final String TAG_GENDER = "rating"; 
       private static final String TAG_ADDRESS = "formatted_address"; 
       private static final String TAG_REFERENCE = "reference"; 


       private static final String TAG_LOCATION = "location"; 
       private static final String TAG_TYPE="type"; 


    // contacts JSONArray 
    JSONArray results = null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      String location = extras.getString("TAG_LOCATION"); 

     // url to make request 
     String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurents+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA"; 
     } 

     LoadData ld = new LoadData(); 
     ld.onPreExecute(); 
    new LoadData().execute(); 

    } 

class LoadData extends AsyncTask<String, String, String> 
    { 
    ProgressDialog pDialog; 

protected void onPreExecute() { 

pDialog = new ProgressDialog(Xyz.this); 
pDialog.setMessage("Populating list please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
} 

    // Hashmap for ListView 
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
    protected String doInBackground(String... args) { 


     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 
     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     try { 
      // Getting Array of Results 
      results = json.getJSONArray(TAG_CONTACTS); 

      // looping through All Contacts 
      for(int i = 0; i < results.length(); i++){ 
       JSONObject c = results.getJSONObject(i); 

       // Storing each json item in variable 
       String id = c.getString(TAG_ID); 
       String name = c.getString(TAG_NAME); 
       String formatted_address = c.getString(TAG_ADDRESS); 
       String rating = c.getString(TAG_GENDER); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_ID, id); 
       map.put(TAG_NAME, name); 
       map.put(TAG_ADDRESS, formatted_address); 
       map.put(TAG_GENDER, rating); 

       // adding HashList to ArrayList 
       contactList.add(map); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
      }  
     protected void onPostExecute(String file_url) { 

     this.pDialog.cancel(); 
       // dismiss the dialog after getting all products 
        /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(Xyz.this, contactList, 
       R.layout.listview_item_row, 
       new String[] { TAG_NAME, TAG_ADDRESS, TAG_GENDER,}, new int[] { 
         R.id.txtTitle, R.id.txtTitle1, R.id.txtTitle3 }); 

     setListAdapter(adapter); 

     // selecting single ListView item 
     ListView lv = getListView(); 

     // Launching new screen on Selecting Single ListItem 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       // Starting new intent 
       Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class); 

      } 
     }); 

     } 

} 
} 

而錯誤是

JSONObject json = jParser.getJSONFromUrl(url); 

我不能把從onCreate()方法任何人都可以修復這個錯誤的URL字符串?

+0

我不明白錯誤是什麼。 – njzk2

回答

2

url是從onCreate方法的局部變量。它甚至不應該在這個狀態下編譯。 (更不用說它可以是未定義的,並且你的AsyncTask仍然被執行)。

您需要通過URL作爲參數傳遞給execute方法:也

String url = args[0]; 

,從的AsyncTask返回null是:

new LoadData().execute(url); 

然後在的AsyncTask,你可以把它拿來不是很有用,返回你獲取的元素數組將是一個好主意。

+0

09-24 15:58:35.233:E/AndroidRuntime(1144):致命異常:的AsyncTask#2 09-24 15:58:35.233:E/AndroidRuntime(1144):java.lang中.RuntimeException –

+0

多一點上下文? – njzk2

3

定義字符串onCreate前...

public class XyzActivity extends ListActivity { 

// JSON Node names 
      private static final String TAG_CONTACTS = "results"; 
      private static final String TAG_ID = "id"; 
      private static final String TAG_NAME = "name"; 
      private static final String TAG_GENDER = "rating"; 
      private static final String TAG_ADDRESS = "formatted_address"; 
      private static final String TAG_REFERENCE = "reference"; 


      private static final String TAG_LOCATION = "location"; 
      private static final String TAG_TYPE="type"; 


// contacts JSONArray 
JSONArray results = null; 

private String url = ""; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     String location = extras.getString("TAG_LOCATION"); 

    // url to make request 
    url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurents+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA"; 
    } 

    LoadData ld = new LoadData(); 

new LoadData().execute(); 

} 

class LoadData extends AsyncTask<String, String, String> 
{ 
ProgressDialog pDialog; 

protected void onPreExecute() { 

pDialog = new ProgressDialog(Xyz.this); 
pDialog.setMessage("Populating list please wait..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(false); 
    pDialog.show(); 
} 

// Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
protected String doInBackground(String... args) { 


    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 
    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     // Getting Array of Results 
     results = json.getJSONArray(TAG_CONTACTS); 

     // looping through All Contacts 
     for(int i = 0; i < results.length(); i++){ 
      JSONObject c = results.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String name = c.getString(TAG_NAME); 
      String formatted_address = c.getString(TAG_ADDRESS); 
      String rating = c.getString(TAG_GENDER); 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_NAME, name); 
      map.put(TAG_ADDRESS, formatted_address); 
      map.put(TAG_GENDER, rating); 

      // adding HashList to ArrayList 
      contactList.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
     }  
    protected void onPostExecute(String file_url) { 

    this.pDialog.cancel(); 
      // dismiss the dialog after getting all products 
       /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(Xyz.this, contactList, 
      R.layout.listview_item_row, 
      new String[] { TAG_NAME, TAG_ADDRESS, TAG_GENDER,}, new int[] { 
        R.id.txtTitle, R.id.txtTitle1, R.id.txtTitle3 }); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    // Launching new screen on Selecting Single ListItem 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class); 

     } 
    }); 

    } 

} 
} 
+0

現在我得到了一個運行時錯誤「FATAL EXCEPTION:AsyncTask#2」,「執行doInBackground()時發生錯誤」 –

+0

你能告訴我導致錯誤的行嗎? btw刪除行(ld.onPreExecute();)不需要調用它,因爲當你執行任務它會自動調用.. – Nermeen

+0

我已經刪除了你上面指定的語句,但仍然有同樣的錯誤 –