GetVolleyResponseという汎用Javaクラスを使用して、volleyを使用してサーバーにデータを送受信します。 同じクラスのprogressDialogを他のすべての呼び出しアクティビティに表示したい。何か試したが動作していない。 GetVolleyResponse.java 別の汎用JavaクラスのProgressDialogを表示
public class GetVolleyResponse {
ProgressDialog progress;
Context ctx;
AlertDialog.Builder alert;
GetVolleyResponse(Context ctx)
{
this.ctx=ctx;
progress=new ProgressDialog(this.ctx);
progress.setTitle("please wait...");
}
public void getResponse(String url, final Map params, final VolleyCallback callback)
{
progress.show();
StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progress.dismiss();
callback.onSuccessResponse(response);//Interface for callback Defined by me..
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progress.dismiss();
String message = null;
if (error instanceof NetworkError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (error instanceof ServerError) {
message = "The server could not be found. Please try again after some time!!";
} else if (error instanceof AuthFailureError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (error instanceof ParseError) {
message = "Parsing error! Please try again after some time!!";
} else if (error instanceof NoConnectionError) {
message = "Cannot connect to Internet...Please check your connection!";
} else if (error instanceof TimeoutError) {
message = "Connection TimeOut! Please check your internet connection.";
}
callback.onErrorResponse(message);
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
};
MySingleton.getInstance(ctx).addToRequestQueue(stringRequest);
}
}
が、progressDialogアプリのクラッシュれるので
..エラーログ:ここ
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.accer.sportsgr, PID: 25143
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accer.sportsgr/com.example.accer.sportsgr.Updater}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:682)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at com.example.accer.sportsgr.GetVolleyResponse.getResponse(GetVolleyResponse.java:41)
at com.example.accer.sportsgr.Updater.onCreate(Updater.java:70)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
はupdater.javaからの呼び出しです
(new GetVolleyResponse(getApplicationContext())).getResponse(Register.serverUrl + UploadUrl, params, new VolleyCallback() {
@Override
public void onSuccessResponse(String result) {
try {
JSONArray jsonArray=new JSONArray(result);
JSONObject jsonObject;
for (int i=0;i<jsonArray.length();i++)
{
jsonObject=jsonArray.getJSONObject(i);
sp_list.add(jsonObject.getString("name"));
}
sp_list.add("others");
setSpinner();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onErrorResponse(String message) {
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
}
});
方法上記のコンストラクタを任意のアクテビリティから呼び出す必要があります
Izzitクラッシュまたは進行状況ダイアログが表示されませんか? –
アプリがクラッシュする – mahendra
このクラスを呼び出すときにgetApplicationContext()を使用してコンテキストを渡していますか? –