Reputation: 777
I'm trying to implement a Tab in my application. So, I have to start the activities as child activities. But problem arises when I start a certain child activity, but not on other activities. So maybe there is something with that Activity. When i compared the two the other one, where the error occurs is executing an asynchronous task. Maybe that is the cause. This is what my logs says:
05-16 18:21:00.475: E/AndroidRuntime(1572): FATAL EXCEPTION: main
05-16 18:21:00.475: E/AndroidRuntime(1572): java.lang.IllegalStateException: Could not execute method of the activity
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.View$1.onClick(View.java:3044)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.View.performClick(View.java:3511)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.View$PerformClick.run(View.java:14105)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.os.Handler.handleCallback(Handler.java:605)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.os.Looper.loop(Looper.java:137)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ActivityThread.main(ActivityThread.java:4446)
05-16 18:21:00.475: E/AndroidRuntime(1572): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 18:21:00.475: E/AndroidRuntime(1572): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-16 18:21:00.475: E/AndroidRuntime(1572): at dalvik.system.NativeStart.main(Native Method)
05-16 18:21:00.475: E/AndroidRuntime(1572): Caused by: java.lang.reflect.InvocationTargetException
05-16 18:21:00.475: E/AndroidRuntime(1572): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 18:21:00.475: E/AndroidRuntime(1572): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.View$1.onClick(View.java:3039)
05-16 18:21:00.475: E/AndroidRuntime(1572): ... 11 more
05-16 18:21:00.475: E/AndroidRuntime(1572): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.project/com.test.project.ProductActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@411d0668 is not valid; is your activity running?
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1978)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1819)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ParentTabActivity.startChildActivity(ParentTabActivity.java:276)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ActivityGroup.showInvoice4(ActivityGroup.java:34)
05-16 18:21:00.475: E/AndroidRuntime(1572): ... 14 more
05-16 18:21:00.475: E/AndroidRuntime(1572): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@411d0668 is not valid; is your activity running?
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.view.Window$LocalWindowManager.addView(Window.java:537)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.Dialog.show(Dialog.java:278)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ProgressDialog.show(ProgressDialog.java:116)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ProgressDialog.show(ProgressDialog.java:99)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ProgressDialog.show(ProgressDialog.java:94)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ProductActivity$BusinessIdListRequestForEverything2.onPreExecute(ProductActivity.java:2984)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.os.AsyncTask.execute(AsyncTask.java:511)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ProductActivity.initialPopulateAllShop(ProductActivity.java:2501)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ProductActivity.populateData3(ProductActivity.java:2408)
05-16 18:21:00.475: E/AndroidRuntime(1572): at com.test.project.ProductActivity.onCreate(ProductActivity.java:250)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.Activity.performCreate(Activity.java:4465)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-16 18:21:00.475: E/AndroidRuntime(1572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1942)
05-16 18:21:00.475: E/AndroidRuntime(1572): ... 19 more
Any enlightenment regarding this matter. Any help is greatly appreciated. Thanks
[EDIT: My code]
This is my MainActivity
:
TabSpec clipBoard = tabHost.newTabSpec("FirstActivity");
Intent firstIntent = new Intent(this, GroupTabActivity.class);
clipBoard.setIndicator(view1);
clipBoard.setContent(firstIntent );
TabSpec photos = tabHost.newTabSpec("ProductActivity");
Intent secondIntent = new Intent(this, GroupTabActivity.class);
photos.setIndicator(view2);
photos.setContent(secondIntent );
Then the GroupTabActivity extends TabParentActivity
TabParentActivity extends to ActivityGroup
public class GroupTabActivity extends ParentTabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("## this1", getParent().getClass().getSimpleName());
startChildActivity("ActivityGroup", new Intent(this,ActivityGroup.class));
}
And lastly, ActivityGroup extends TabParentActivity
public void onButtonClick1(View v){
Intent intent = new Intent(getParent(), FirstActivity.class);
TabParentActivity parentActivity = (TabParentActivity)getParent();
parentActivity.startChildActivity("FirstActivity", intent);
}
public void onButtonClick2(View v){
Intent intent = new Intent(getParent(), ProductActivity.class);
TabParentActivity parentActivity = (TabParentActivity)getParent();
parentActivity.startChildActivity("ProductActivity", intent);
}
It is after onclick2 that i encounter that error. ProductActivitycontains an asynctask.
In the log, line 2984: In ProductActivity:
@Override
protected void onPreExecute() {
if (mainProgressDialog == null){
mainProgressDialog = ProgressDialog.show(ProductActivity.this, "", getString(R.string.loading));
}else{
if (mainProgressDialog.isShowing()==false){
mainProgressDialog = ProgressDialog.show(ProductActivity.this, "", getString(R.string.loading));
}
}
super.onPreExecute();
}
UPDATE: AsyncTask:
private class BusinessIdListRequestForEverything2 extends AsyncTask<String, Void, String>{
ApiService service;
String mode = ""; //either all or shop
String indicator = "";
List <BusinessDetailsRequestForEverything2> businessRequestList = null;
int numberOfBusinessRequests = 0;
public String getIndicator() {
return indicator;
}
public void setIndicator(String indicator) {
this.indicator = indicator;
}
public ApiService getService() {
return service;
}
public void setService(ApiService service) {
this.service = service;
}
public BusinessIdListRequestForEverything2(ApiService service){
this.service = service;
businessRequestList = new ArrayList<BusinessDetailsRequestForEverything2>();
}
@Override
protected String doInBackground(String... params) {
service.execute(RequestMethod.POST);
return null;
}
/**
* checks all getBusiness api requests if finished
*
* @return
*/
public boolean isBusinessRequestsFinished(){
boolean retVal = false;
int counter = 0;
if (businessRequestList!=null && businessRequestList.size()>0){
for(BusinessDetailsRequestForEverything2 request: businessRequestList){
if (request.getStatus()==AsyncTask.Status.FINISHED){
counter++;
}
}
if (counter == numberOfBusinessRequests){
retVal = true;
}
}
return retVal;
}
public void stopAllBusinessDetailsRequestEverything(){
if (businessRequestList!=null && businessRequestList.size()>0){
for(BusinessDetailsRequestForEverything2 request: businessRequestList){
if (request.getStatus()==AsyncTask.Status.RUNNING){
request.cancel(true);
}
}
}
}
@Override
protected void onPostExecute(String result) {
try{
String response = service.getResponse();
if (response!=null){
Log.d("fritz", "BusinessIdListRequestForEverything response is "+response);
JSONObject jsonObject = new JSONObject(service.getResponse());
JSONObject project = jsonObject.getJSONObject("project");
JSONArray jsonArray = project.getJSONArray("shops");
if (jsonArray.length() > 0) {
getBusinessIdListRequestForEverythingCounter=0;
getBusinessIdListRequestForEverythingBusinessCount = jsonArray.length();
numberOfBusinessRequests = jsonArray.length();
//clear the businessRequestList
businessRequestList.clear();
//increment offset by limit
getBusinessIdOffset += getBusinessIdLimit;
//empty the businessRequestList
for (int x = 0; x < jsonArray.length(); x++) {
JSONObject temp = jsonArray.getJSONObject(x);
Log.d("fritz", "business id =" + temp.getInt("id"));
//getBusiness(temp.getInt("id"));
//call getBusiness api
MottocoApi projectApi = application.getMottocoApiFactory();
ApiService getBusiness = projectApi.getApi("getBusiness");
getBusiness.addParam("id", String.valueOf(temp.getInt("id")));
BusinessDetailsRequestForEverything2 request = new BusinessDetailsRequestForEverything2(getBusiness);
request.execute();
businessRequestList.add(request);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
super.onPostExecute(result);
}
Upvotes: 0
Views: 281
Reputation: 22232
WindowManager$BadTokenException
happens when you try to show dialog on Activity
that is already destroyed.
I'm not sure how this can happen in your AsyncTask.onPreExecute
, but for sure there is a (temporal) memory leak in your code.
I would suggest not using deprecated API of ActivityGroup
with friends and switch to Fragments
.
Upvotes: 1