AsyncTask在4.0.4中不起作用

时间:2015-02-06 17:40:55

标签: android android-asynctask

我有一个使用谷歌地图的应用程序,我试图通过AsyncTask获取JSON数据来获取持续时间数据...并在我的选项卡上测试...

AsyncTask无效并在此处执行...

通过一些搜索我知道AsyncTask在4.0.4中不能正常工作所以你最好使用runnable而不是AsyncTask

现在我不知道runnable或者处理程序和所有......

请帮助我如何以及在哪里以及我需要编写什么来处理我的AsyncTask?

public class Map extends Activity {

    GoogleMap mMap;
    String duration,distance,url,s;
    StringBuilder data = null;
    InputStream is = null;
    JSONObject jObj = null;
    String json = "";
    ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());


            //Toast.makeText(getApplicationContext(), "Cant connect to google play service", 10).show();    
            MapFragment fa = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment1);
            mMap = fa.getMap();

            new dis().execute();
            //Toast.makeText(getApplicationContext(), duration, 10).show();


    }

    public class dis extends AsyncTask<Void, Void, Void>    {

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            try{
                Toast.makeText(getApplicationContext(), "jinal", 10).show();
            }catch(Exception e){}

            return null;
            }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub

            pd.dismiss();
            Toast.makeText(getApplicationContext(), "jinal", 10).show();
                super.onPostExecute(result);


        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = new ProgressDialog(Map.this);
            pd.setTitle("Please Wait....");
            pd.setMessage("Wait");
            pd.setCancelable(false);
            pd.show();
        }


    }


}

现在我只是简单的代码,但它不提供输出

2 个答案:

答案 0 :(得分:0)

您应该在doInBackground()方法的 try / catch 语句中添加断点,并查看它是否实际执行。然后在catch块中放入e.printStackTrace(),看看是否有例外!

答案 1 :(得分:0)

Toast需要Activity上下文,而不是Application one

你应该尝试:

Toast.makeText(Map.this, "jinal", Toast.LENGTH_SHORT).show();