当我调用异步类时,不是Android中的Enclosing类

时间:2016-08-17 10:39:04

标签: java android android-asynctask

实际上当我从我的MainActivity调用Async类时,我得到了错误(“不是一个封闭的类”)并尽可能努力但无法将此问题简化

MainActivity Code:

 Button listview=(Button)findViewById(R.id.inListView);
        listview.setOnClickListener(new View.OnClickListener() {
            String Restaurant = "restaurant";
            @Override
            public void onClick(View v) {
                Log.d("onClick", "Button is Clicked");

                String url = getUrl(latitude, longitude, Restaurant);
                Object[] DataTransfer = new Object[2];
                DataTransfer[0] = mMap;
                DataTransfer[1] = url;
                Log.d("onClick", url);
                listviewActivity.GetNearbyPlaceData getNearbyPlacesListdata = new listviewActivity.GetNearbyPlaceData();
                getNearbyPlacesListdata.execute(DataTransfer);
                Toast.makeText(MapsActivity.this,"Nearby Restaurants", Toast.LENGTH_LONG).show();

            }
        });

AsyncTask代码:

public class listviewActivity extends AppCompatActivity {

    static ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>();
    // KEY Strings

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        Intent intent = getIntent();
        // Getting listview
        lv = (ListView) findViewById(R.id.list);
    }


    private void ShowNearbyPlaces(List<HashMap<String, String>> nearbyPlacesList) {
        placesListItems.add((HashMap<String, String>) nearbyPlacesList);
        // list adapter
        ListAdapter adapter = new SimpleAdapter(listviewActivity.this, placesListItems,
                R.layout.list_item,
                new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                R.id.reference, R.id.name });

        // Adding data into listview
        lv.setAdapter(adapter);
    }


    public class GetNearbyPlaceData extends AsyncTask<Object, String, String> {
        private GoogleMap mMap;
        private String url;
        private String googlePlacesData;


        @Override
        protected String doInBackground(Object... params) {
            try {
                Log.d("GetNearbyPlacesData", "doInBackground entered");
                mMap = (GoogleMap) params[0];
                url = (String) params[1];
                DownloadUrl downloadUrl = new DownloadUrl();
                googlePlacesData = downloadUrl.readUrl(url);
                Log.d("GooglePlacesReadTask", "doInBackground Exit");
            } catch (Exception e) {
                Log.d("GooglePlacesReadTask", e.toString());
            }
            return googlePlacesData;
        }

        @Override
        protected void onPostExecute(String result) {
            Log.d("GooglePlacesReadTask", "onPostExecute Entered");
            List<HashMap<String, String>> nearbyPlacesList = null;
            DataParser dataParser = new DataParser();
            nearbyPlacesList =  dataParser.parse(result);
            ShowNearbyPlaces(nearbyPlacesList);
            Log.d("GooglePlacesReadTask", "onPostExecute Exit");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的&#34; GetNearbyPlaceData&#34;是一个内部类,这意味着它只能存在于外部类的实例的上下文中(&#34; listviewActivity&#34;在这种情况下)。要实例化它,您需要一个活动实例,不建议这样做。 将GetNearbyPlaceData移动到您使用它的类,将其移动到它自己的文件,或使其静态。