.execute无法解析为一个类型--AsyncTask(Android)

时间:2014-06-09 09:54:00

标签: java android json eclipse android-asynctask

我正在编写一个需要从我的数据库中获取一些json的应用程序,我会收到数据,但现在我还试图在列表视图中显示的信息旁边查看一个图标。

制造麻烦的路线是:

mChart.setTag(URL);
new DownloadImagesTask.execute(mChart); <------

MainActivity:

public class MainActivity extends Activity {

  ListView list;
  TextView icon;
  TextView name;
  TextView developer;
  TextView size;

  Button Btngetdata;

  ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();

  private static String url = "http://appwhittle.com/getdata.php";

  private static final String TAG_ITEM = "app_item";
  private static final String TAG_ICON = "app_icon";
  private static final String TAG_NAME = "app_name";
  private static final String TAG_DEVELOPER = "app_developer";
  private static final String TAG_SIZE = "app_size";

  JSONArray mJsonArray = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList = new ArrayList<HashMap<String, String>>();

        new JSONParse().execute();
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
       private ProgressDialog pDialog;
      @Override
        protected void onPreExecute() {
            super.onPreExecute();

            icon = (TextView)findViewById(R.id.icon);
            name = (TextView)findViewById(R.id.name);
            size = (TextView)findViewById(R.id.size);
            developer = (TextView)findViewById(R.id.developer);

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
      }

      @Override
        protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        return json;
      }
       @Override
         protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
            mJsonArray = json.getJSONArray(TAG_ITEM);
            for(int i = 0; i < mJsonArray.length(); i++){
            JSONObject c = mJsonArray.getJSONObject(i);

            String name = c.getString(TAG_NAME);
            String size = c.getString(TAG_SIZE);
            String developer = c.getString(TAG_DEVELOPER);
            String icon = c.getString(TAG_ICON);

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_ICON, icon);
            map.put(TAG_NAME, name);
            map.put(TAG_DEVELOPER, "Developer: " + developer);
            map.put(TAG_SIZE, size + " MB");                


            mList.add(map);
            list=(ListView)findViewById(R.id.list);

            ListAdapter adapter = new SimpleAdapter(MainActivity.this, mList,
                R.layout.list_v,
                new String[] {TAG_NAME, TAG_DEVELOPER, TAG_SIZE }, new int[] {
                    R.id.name, R.id.developer, R.id.size});

            ImageView mChart = (ImageView) findViewById(R.id.icon);
            String URL = "http://www...anything ...";

            mChart.setTag(URL);
            new DownloadImagesTask.execute(mChart);

            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+ mList.get(+position).get(TAG_NAME), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
          e.printStackTrace();
        }
       }
    }        
}

DownloadImagesTask:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }


    private Bitmap download_Image(String url) {
        return null;
        }
    }

非常感谢任何帮助!提前谢谢!

5 个答案:

答案 0 :(得分:43)

替换

new DownloadImagesTask.execute(mChart); 

new DownloadImagesTask().execute(mChart); 

试试这个。它会起作用。

答案 1 :(得分:2)

您需要更改

new DownloadImagesTask.execute(mChart);

new DownloadImagesTask().execute(mChart);.

更改后检查,希望它工作正常。快乐的编码:)

答案 2 :(得分:0)

变化:

new DownloadImagesTask.execute(mChart); 

为:

new DownloadImagesTask(mChart).execute(); 

答案 3 :(得分:0)

您尚未通过调用构造函数初始化DownloadImagesTask的对象。

new DownloadImagesTask().execute(mChart);像这样使用。通过放置()来调用默认构造函数。

答案 4 :(得分:0)

//Suppose you have a class "DataFetcher" which extends  "AsyncTask<Void,Void,Void>{}"

//to execute this class write the code as follows

new DataFetcher().execute();  //done