0

私はAndroidアプリケーションにAsyncTaskクラスを持っています。私のMainActivityは、そのクラスのメソッドを呼び出しますが、そうした場合、そのクラスのProgressDialogボックスに問題があります。 LoginSoapTask.javaAndroid AsyncTask with ProgressDialog - Nullオブジェクトリファレンス

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

:ここ

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) 

は、両方のクラスのコードの主要部分である:

この

は私が取得していますエラーです
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(); 
    } 
} 

私は引用符を挿入して、「これは私が問題を抱えているところです」と言って、ちょっと頭を上げ始めました。

これを解決する方法がわかっている場合、PLEASE他のソリューションのリンクを投稿しないでください。この問題を直接解決できるコードで回答を投稿してください。私は文脈と関連しなければならない他の解決策を見てきましたが、私はどこにもいません。代わりに

LoginSoapTask lert = new LoginSoapTask(getApplicationContext()); 

+0

onPreExcute()で進捗ダイアログを作成します。 –

+0

また、コンテキスト= cをthis.context = cに変更します。 –

+0

@ Chosen1あなたはアプリケーション 'Context'を使うべきではありません。代わりに' this'を試してください。 – Bryan

答えて

0

判明:

Context context; 
private final ProgressDialog dialog; 

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

また、あなたはそれが簡単にビューを使用するために見つけるかもしれないあなたのclickListener中(v)のparamのコンテキストは、ここでの例でありますそれをLoginSoapTaskに転送します。皆さんがどのように設定しているかとは少し異なります。私はそれを別の公開方法として使用しました。

0

これは

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

MainActivity

に置き換えてみてください

LoginSoapTask lert = new LoginSoapTask(getApplicationContext()); 

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

コンストラクタを少し変更して、内部にダイアログを作成します。私はMainActivityのパブリックインスタンスを作成しておくこと

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()); 
    } 
}); 
関連する問題