android - 无法访问android中的父类(super class)成员

标签 android class string-concatenation

<分区>

在我的 android 项目中有一个小错误,我无法从 AsyncTask 子类访问 Super 类字符串变量,我得到以下错误

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 字符串,谁能解决这个错误?

最佳答案

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);

        }
    });

    }

}
}

关于android - 无法访问android中的父类(super class)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564606/

相关文章:

php - java.lang.String类型的值<br的错误无法转换为JSONObject

c# - UML 类图 - 我们在这里包含像用户控件和 MainWindow 这样的部分类吗?

c++ - 通过 "a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)

postgresql - for EXECUTE format() 中整数变量的格式说明符?

sql - Oracle中的字符串连接运算符是什么?

java - 在 Java 中连接字符串是否总是会导致在内存中创建新字符串?

java - Android - 单击 ActionBar 按钮时创建 PopupMenu

android - 值错误 : Linkname is too long

Android:无法更改 AlertDialog 中出现的文本

python 对象从错误的文本文件中获取先前的对象属性,并从正确的文本文件中获取它们自己的属性