2012-05-08 9 views
5

Ich habe 3 Klassen und die Klasse namens WebServiceClint Klasse erweitert Asynctask und in doInBackgrnd() Ich überlasse URL und ich erhalte Daten von Webservice. aber ich rufe dies von der Methode einer anderen Klasse namens VerifyTeacherId. Wie kann ich Fortschrittsdialog zeigen ??? wo soll ich pg.show und pg.dismiss schreiben ???Fortschrittsdialog mit asynctask

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

Und die Methode, wo ich bin Aufruf ist es in einer anderen Klasse mit dem Namen SelectOptionActivity. Das Verfahren ist

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

Jetzt Fehler immer wie

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 

Antwort

11

Pass Rahmen Ihrer aktuellen Aktivität Klasse AsyncTask und die Verwendung dieser Kontext zeigen Fortschrittsdialog in onPreExecute() und entlässt es 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(); 
     } 

. 
. 
. 

Code ist nur für Ihr Verständnis.

+1

+1 ich würde mit dieser Lösung gehen. –

+0

Sir, ich möchte das aus verschiedenen Aktivitäten nennen. So ist es die richtige Art, wie WebServiceClient aufzurufen clientThread = new WebServiceClient (SelectOptionActivity.this); weil ich wieder Fehler bekomme – samir

+0

@ user370305 Sir schrieb den Fehler. Plz help – samir

2

Die Instanz von Activity ist null, weil Sie vor der Funktion oncreate ein Objekt des WebServiceClient erstellen. Während die Aktivität oder der Kontext übergeben wird, muss sichergestellt werden, dass die Aktivität anderweitig erstellt wurde. Es wird null übergeben und Sie erhalten diesen Fehler

+0

danke Kumpel. war wirklich hilfreich. Leider hatte ich nur eine Akzeptanz. – samir

+0

@samirsingh können Sie die Antworten abstimmen –

Verwandte Themen