5

我有 3 个类,名为 WebServiceCleint 的类正在扩展Asynctask并在doInBackgrnd()我传递 url 和我从 web 服务获取数据。但我从另一个名为 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
4

2 回答 2

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

.
.
.

代码仅供您理解..

于 2012-05-08T06:26:19.783 回答
2

Activity 的实例为空,因为您是在 oncreate 函数之前创建 WebServiceClient 的对象。在传递 Activity 或 Context 时,请确保 Activity 已创建其他方式,否则将传递 null 并且您将收到此错误

于 2012-05-08T07:16:33.217 回答