ProgressDialog with Threadsの質問が何度も行われていることは知っていますが、私のプロジェクトではどのソリューションも機能していないようです。基本的に私がしたいのはこれです:1)ユーザーがボタンをクリックするとアクティビティがサーバーに認証リクエストを送信します2)これが行われている間ProgressDialogが表示されます3)応答が来たらProgressDialogを却下したいと思いますアクティビティによって読み取られ、解釈されるリターンオブジェクト
Iの場合:1)応答でアプリケーションフィールドを更新するようにスレッドを設定すると、フィールドにアクセスするときに次のメソッド(スレッドの外側)がNPEをスローします2)スレッドに次のメソッドを含めると2番目のメソッド'java.lang.RuntimeException:Looper.prepare()を呼び出していないスレッド内にハンドラーを作成できません'をスローします
長いテキストで申し訳ありませんが、私はこれで完全にそれを失っています...私のコードは次のようになります:
public class XXX extends Activity implements OnClickListener {
// (...)
private SoapObject returnObject;
private String response;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// (...)
authProgressDialog = ProgressDialog.show(XXX.this, "", "Authenticating...", true, false);
new Thread(new Runnable() {
@Override
public void run() {
authenticate(); // method that calls the API via SOAP
authenticateReal(); // method that handles the response
}
}).start();
mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 10:
authProgressDialog.dismiss();
break;
}
}
};
}
}
public void authenticate() {
// API stuff (...)
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
returnObject = (SoapObject) soapEnvelope.getResponse();
response = returnObject.getProperty("ResponseStatus").toString();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
mHandler.sendEmptyMessage(10);
}
}
// Method that needs to access returnObject and reponse objects and
// it is here where the NPE's or other exceptions are thrown
public void authenticateReal() {
// (...)
}