1

使用 JSON 捕获数据时,我正在尝试进行进度对话框。但它总是返回一个空值。这是代码:

class NetWork extends AsyncTask<Void, Void, ArrayAdapter<?>> {

        private ProgressDialog pd;

        public void Network() {
            try {
                ServiceStations service = new ServiceStations();
                service.load(WorkshopActivity.this.getApplicationContext());
            } catch (IOException e) {
                // do nothing
                e.printStackTrace();
            } catch (JSONException e) {
                // do nothing
                e.printStackTrace();
            }
            pd = new ProgressDialog(WorkshopActivity.this);
            pd.setMessage("Load");
            pd.setCancelable(false);
            pd.setCanceledOnTouchOutside(false);

        }

        @Override
        public ArrayAdapter<?> doInBackground(Void... params) {


            @SuppressWarnings({ "unchecked", "rawtypes" })
            final ArrayAdapter<?> areaAdapter = new ArrayAdapter(
                    WorkshopActivity.this, android.R.layout.simple_list_item_1,
                    ServiceStations.getAreaList());

            return areaAdapter;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                pd.show(); // this returns null
            } catch (NullPointerException e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }

        @Override
        public void onPostExecute(ArrayAdapter<?> areaAdapter) {
            /*
             * if (dialog.isShowing()) { dialog.dismiss(); }
             */
            list = (ListView) findViewById(R.id.listWorkshopCity);
            list.setAdapter(areaAdapter);
            adapter = areaAdapter;
        }

    }

这是堆栈跟踪:

07-15 04:34:15.006: W/System.err(3393): java.lang.NullPointerException
07-15 04:34:15.006: W/System.err(3393):     at com.jatismobile.iklaim.activities.WorkshopActivity$NetWork.onPreExecute(WorkshopActivity.java:74)
07-15 04:34:15.026: W/System.err(3393):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-15 04:34:15.026: W/System.err(3393):     at android.os.AsyncTask.execute(AsyncTask.java:534)
07-15 04:34:15.036: W/System.err(3393):     at com.jatismobile.iklaim.activities.WorkshopActivity.onCreate(WorkshopActivity.java:102)

我一直在尝试搜索互联网,但我已经找到了解决方案。任何帮助都将不胜感激。谢谢..

4

1 回答 1

2

你有这个

 public void Network()

构造函数没有返回类型

它应该是

 public Network()
 {
      // rest of the code
 }    

您在未调用的方法中初始化您的进度对话框。您不要在课堂上的任何地方调用方法 Network 。因此NullPointerException

于 2013-07-15T04:48:52.780 回答