0

Ich habe eine AsyncTask Klasse in meiner Android App. Meine MainActivity ruft eine Methode in dieser Klasse auf, aber wenn ich das tue, hat es ein Problem mit dem ProgressDialog-Feld darin.Android AsyncTask mit ProgressDialog - Null Objektreferenz

Dies ist der Fehler Ich erhalte:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference 
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222) 
at android.app.AlertDialog.<init>(AlertDialog.java:200) 
at android.app.AlertDialog.<init>(AlertDialog.java:196) 
at android.app.AlertDialog.<init>(AlertDialog.java:141) 
at android.app.ProgressDialog.<init>(ProgressDialog.java:77) 
at com.example.maindec.sme.LoginSoapTask.<init>(LoginSoapTask.java:30) 
at com.example.maindec.sme.MainActivity$3.onClick(MainActivity.java:103) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Hier die wichtigsten Teile des Codes in beiden Klassen:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private String username; 
    private String password; 

    private EditText usernameField; 
    private EditText passwordField; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     usernameField = (EditText) findViewById(R.id.enterUsername); 
     passwordField = (EditText) findViewById(R.id.enterPassword); 

     passwordField.setTypeface(Typeface.DEFAULT); 
     passwordField.setTransformationMethod(new PasswordTransformationMethod()); 


     loginButton.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 

        LoginSoapTask lert = new LoginSoapTask(getApplicationContext()); //THIS IS WHERE I'M HAVING THE PROBLEM (Line 103) 
        // lert.checkDetails(usernameField.getText().toString(), passwordField.getText().toString()); 
       } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

LoginSoapTask.java

public class LoginSoapTask extends AsyncTask<Void, Void, Void> { 
    /*Web Service Details */ 

    private String xmlProlog = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 

    public String username, password; 

    Context context; 
    public LoginSoapTask(Context c) { 
     context = c; 
    } 
    private final ProgressDialog dialog = new ProgressDialog(context); //THIS IS WHERE I AM HAVING THE PROBLEM!!!!!!!!! (Line 30) 
    protected void onPreExecute() { 

     this.dialog.setMessage("Verifying..."); 
     dialog.show(); 
    } 


    @Override 
    protected Void doInBackground(Void... params) { 
     return null; 
    } 

    protected void onPostExecute(Void result) { 
     // TextView tv = (TextView) findViewById(R.id.Result); 
     // tv.setText(test_string); 
     System.out.println(test_string); 
     if (this.dialog.isShowing()){ 
      this.dialog.dismiss(); 
     } 
    } 

    private String test_string; 

    @SuppressWarnings("deprecation") 
    public void WebServiceCallExample() { 
     String finalInput = "1:" + username.length() + ":" + username + ":2:" + password.length() + ":" + password + ":"; 
     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
     Request.addProperty("credentials", finalInput); 

     /* Set the web service envelope */ 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 
     System.out.println("Request::" + Request.toString()); 

     HttpTransportSE httpTransport = new HttpTransportSE(URL); 
     try{ 
      System.out.println("Lghung:" + SOAP_ACTION); 
      httpTransport.debug=true; 

      httpTransport.call(SOAP_ACTION, envelope); 
      String ss=httpTransport.responseDump; 
      //SoapObject sp = (SoapObject) envelope.getResponse(); 
      SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 

      SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn; 
      System.out.println("Response::" + resultsRequestSOAP.toString()); 
      System.out.println(Request.getName()); 
      test_string = xmlProlog + response.toString(); 
      System.out.println(test_string); 
     }catch (Exception e){ 
      System.out.println("Testing"); 
      e.printStackTrace(); 
     } 

    } 

    public void checkDetails(String givenUsername, String givenPassword) { 
     username = givenUsername; 
     password = givenPassword; 
     execute(); 
    } 
} 

Ich habe Zitate eingefügt, die sagen "DAS IST WO ICH DAS PROBLEM HABE", um Ihnen einen kleinen Vorsprung zu geben.

Wenn Sie wissen, wie das zu lösen ist, BITTE keine Links für andere Lösungen. Veröffentlichen Sie eine Antwort mit Code, der dieses Problem direkt lösen kann. Ich habe mir andere Lösungen angeschaut, die mit dem Kontext zu tun haben, aber ich komme nirgendwohin.

+0

erstellen Sie Ihren Fortschrittsdialog in onPreExcute() –

+0

ändern Sie auch context = c zu this.context = c; –

+0

@ Chosen1 Sie sollten nicht die Anwendung 'Context' verwenden, versuchen Sie' this' stattdessen. – Bryan

Antwort

0

Es stellt sich heraus, dass ich eine öffentliche Instanz eines MainActivity machen und übertrage es auf LoginSoapTask. Etwas anders als wie ihr es aufgebaut habt. Ich habe es als eine andere öffentliche Methode benutzt.

0

statt

LoginSoapTask lert = new LoginSoapTask(getApplicationContext()); 

versuchen diese

LoginSoapTask lert = new LoginSoapTask(MainActivity.this); 
2

in MainActivity

ersetzen

LoginSoapTask lert = new LoginSoapTask(getApplicationContext()); 

mit

LoginSoapTask lert = new LoginSoapTask(MainActivity.this); 
0

Ändern Sie Ihren Konstruktor leicht, um auch den Dialog darin zu erstellen.

Context context; 
private final ProgressDialog dialog; 

public LoginSoapTask(Context c) { 
    context = c; 
    dialog = new ProgressDialog(context); 
} 

Auch Sie könnte es einfacher finden, um die Ansicht zu verwenden (v) Kontext des param in Ihren clicklistener, hier ein Beispiel:

loginButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     LoginSoapTask lert = new LoginSoapTask(v.getContext()); 
     // lert.checkDetails(usernameField.getText().toString(), passwordField.getText().toString()); 
    } 
}); 
Verwandte Themen