Momo1199 2013-07-15 05:44 采纳率: 0%
浏览 2464
已采纳

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条回答 默认 最新

  • 码密码密哄 2013-07-15 14:28
    关注

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

    try {
    if(pd == null) {
    //create new dialog
    } 
                pd.show(); // this returns null
            } catch (NullPointerException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏