与asynctask的进度对话框

时间:2012-05-08 06:18:32

标签: android android-asynctask progressdialog

我有3个类,名为WebServiceCleint的类正在扩展Asynctask,而doInBackgrnd()我正在传递url,我从webservice获取数据。但我从另一个名为VerifyTeacherId的类中调用它。现在我怎样才能显示进度对话框???我应该在哪里写pg.show和pg.dismiss。???

public  class WebServiceClient extends AsyncTask<String, Void, String>
{

 private static final String base_path = "http://www.gdaschools.in/";
 protected static final String SLASH = "/";


 private ProgressDialog dialog;
 private Activity activity;

 public WebServiceClient(Activity activity) {
        this.activity = activity;
        this.dialog = new ProgressDialog(activity);
    }

    @Override
     protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }



@Override
protected String doInBackground(String... params) {

    StringBuffer sb = new StringBuffer();
    sb.append(base_path);
    sb.append(params[0]);
    HttpRetriever retrieveResponse = new HttpRetriever();
    retrieveResponse.retrieve(sb.toString());
    return retrieveResponse.getResponseXml();
}



 @Override
protected void onPostExecute(String result) {
     if (this.dialog.isShowing()) {
           this.dialog.dismiss();
     }
}
}

我调用的方法是另一个名为SelectOptionActivity的类。方法是

public void verifyTeacherId(View view)
{
    teacherIdString = TeacherId.getText().toString().trim();

    clientThread = new WebServiceClient(SelectOptionActivity.this);
    clientThread.execute("teacher/" + teacherIdString);     

    try 
    {
        String xml = clientThread.get();

        DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
        factory1.setNamespaceAware(true);
        try
        {
            DocumentBuilder builder = factory1.newDocumentBuilder();
            Document doc =builder.parse(new InputSource(new StringReader(xml))); 
            Element root = doc.getDocumentElement();
            if (doc != null) 
            {
                    NodeList nl = doc.getElementsByTagName("empId");
                    if (nl.getLength() > 0)
                    {
                        Node node = nl.item(0);
                        responseTeacherId = node.getTextContent();
                    }
                    NodeList n2=doc.getElementsByTagName("empName");
                    if (n2.getLength() > 0)
                    {
                        Node node = n2.item(0);
                        responseTeacherName = node.getTextContent();

                    }
            }

             Toast.makeText(getBaseContext(),""+responseTeacherId,10).show();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

现在收到错误

05-08 12:10:10.834: D/AndroidRuntime(524): Shutting down VM
05-08 12:10:10.834: W/dalvikvm(524): threadid=1: thread exiting with uncaught exception (group=0x40014760)
05-08 12:10:10.872: E/AndroidRuntime(524): FATAL EXCEPTION: main
05-08 12:10:10.872: E/AndroidRuntime(524): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.GDAProj/com.GDAProj.SelectOptionActivity}: java.lang.NullPointerException
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1739)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.os.Looper.loop(Looper.java:132)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread.main(ActivityThread.java:4123)
05-08 12:10:10.872: E/AndroidRuntime(524):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 12:10:10.872: E/AndroidRuntime(524):  at java.lang.reflect.Method.invoke(Method.java:491)
05-08 12:10:10.872: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-08 12:10:10.872: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-08 12:10:10.872: E/AndroidRuntime(524):  at dalvik.system.NativeStart.main(Native Method)
05-08 12:10:10.872: E/AndroidRuntime(524): Caused by: java.lang.NullPointerException
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:120)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.AlertDialog.<init>(AlertDialog.java:80)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
05-08 12:10:10.872: E/AndroidRuntime(524):  at com.GDAProj.WebServiceClient.<init>(WebServiceClient.java:20)
05-08 12:10:10.872: E/AndroidRuntime(524):  at com.GDAProj.SelectOptionActivity.<init>(SelectOptionActivity.java:70)
05-08 12:10:10.872: E/AndroidRuntime(524):  at java.lang.Class.newInstanceImpl(Native Method)
05-08 12:10:10.872: E/AndroidRuntime(524):  at java.lang.Class.newInstance(Class.java:1301)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
05-08 12:10:10.872: E/AndroidRuntime(524):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1730)
05-08 12:10:10.872: E/AndroidRuntime(524):  ... 11 more

2 个答案:

答案 0 :(得分:11)

将当前活动的上下文传递给AsyncTask 类,并在onPreExecute()中使用该上下文显示进度对话框并将其关闭onPostExecute()

public  class WebServiceClient extends AsyncTask<String, Void, String>
{
 private static final String base_path = "http://www.gdaschools.in";
 protected static final String SLASH = "/";
 private ProgressDialog dialog;
 private Activity activity;

public WebServiceClient(Activity activity) {
    this.activity = activity;
    this.dialog = new ProgressDialog(activity);
}

@Override
 protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

@Override
    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
           this.dialog.dismiss();
        }

.
.
.

代码仅供您理解..

答案 1 :(得分:2)

Activity的实例为null,因为您在oncreate函数之前创建了WebServiceClient的对象。在传递Activity或Context时,请确保Activity已经创建了其他明智的方法,它将传递null并且您将收到此错误