2 momo1199 Momo1199 于 2013.07.15 13:44 提问

Android ProgressDialog返回空

想要做一个进程对话框用在JSON捕捉数据时。但是返回空值。代码:

class NetWork extends AsyncTask> {

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

stacktrace:

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)

1个回答

cj6585256
cj6585256   2013.07.15 22:28
已采纳

加个null判断就好了,null的时候创建

try {
if(pd == null) {
//create new dialog
} 
            pd.show(); // this returns null
        } catch (NullPointerException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!