Ashish Kudale Ashish Kudale -4 years ago 139
Java Question

App crashes due to IllegalArgumentException : com.android.internal.policy.impl.PhoneWindow

On the basis of webservice response I am deciding to open activity.

if(getCompanyByUser().size() > 0) {
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
startActivity(intent);
finish();
}


getCompanyByUser() is this method where I am calling webservice.

private ArrayList<CompApp> getCompanyByUser() {
final ArrayList<CompApp> list = new ArrayList<CompApp>();
showpDialog(pDialog);
StringRequest stringRequest = new StringRequest(Request.Method.POST, getCompByUser,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response",response);
try{
CompApp[] access = new Gson().fromJson(response, CompApp[].class);
for (CompApp c : access) {
list.add(c);
}

helper.addCompany(list);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String dateString = formatter.format(new java.util.Date());
saveSharedPref(SplashScreenActivity.this, "timestamp", dateString);
}catch (Exception e) {
Toast.makeText(SplashScreenActivity.this, "Unable to process request.\nTry again.", Toast.LENGTH_SHORT).show();
}
hidepDialog(pDialog);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SplashScreenActivity.this, "Internet not connected.", Toast.LENGTH_LONG).show();
hidepDialog(pDialog);
}
}) {
@Override
protected Map<String,String> getParams() {
Map<String,String> params = new HashMap<String, String>();
User user = new Gson().fromJson(getSharePref(SplashScreenActivity.this, "user"), User.class);
String lastUpdated = getSharePref(SplashScreenActivity.this, "lastUpdated");
params.put("appCode","PCA001");
params.put("userId",user.id);
params.put("timestamp",""+lastUpdated);
return params;
}
};

RequestQueue requestQueue = Volley.newRequestQueue(SplashScreenActivity.this);
requestQueue.add(stringRequest);
return list;
}


Now In this list is always null because it is being filled in background. So Now It will always open SelectCompany activity.

I have two problems my app is crashing due to
java.lang.IllegalArgumentException
and I want program should wait till list get filled.

Here is
logcat


java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{42130a30 V.E..... R......D 0,0-456,144} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
at android.app.Dialog.dismissDialog(Dialog.java:337)
at android.app.Dialog.dismiss(Dialog.java:320)
at com.ashishkudale.linksolution.common.CommonMethods.hidepDialog(CommonMethods.java:27)
at com.ashishkudale.linksolution.SplashScreenActivity$11.onResponse(SplashScreenActivity.java:339)
at com.ashishkudale.linksolution.SplashScreenActivity$11.onResponse(SplashScreenActivity.java:321)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5127)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)

Answer Source

Flow of the code is incorrect here. When you call getCompanyByUser(), it is an async call. Hence you need to wait for its response before your proceed.

So simply call getCompanyByUser(). Change its return type to void because that is not required since it is an async call. Now, You should start your activities in onResponse and ErrorResponse callbacks.

private void getCompanyByUser() {
//....
@Override
public void onResponse(String response) {
 // ...
//Once the list is made start new activity
if(list.size() > 0) {
    Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
} else {
    Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
    startActivity(intent);
    finish();
}
}
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download