0

我正在尝试在我的应用程序中实现一个选项卡。因此,我必须将活动作为子活动开始。但是当我开始某个儿童活动时出现问题,而不是其他活动。所以也许那个活动有什么东西。当我比较两者时,另一个发生错误的地方是执行异步任务。也许这就是原因。这就是我的日志所说的:

    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

关于此事的任何启示。任何帮助是极大的赞赏。谢谢

[编辑:我的代码]

这是我的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 );

然后GroupTabActivity extends TabParentActivity TabParentActivity 扩展到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));
        }

最后,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);

        }

在 onclick2 之后我遇到了那个错误。ProductActivity 包含一个异步任务。

在日志中,第 2984 行:在 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();
    }

更新:异步任务:

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

1 回答 1

1

WindowManager$BadTokenException当您尝试显示Activity已销毁的对话框时会发生这种情况。

我不确定这如何发生在您AsyncTask.onPreExecute的 .

我建议不要ActivityGroup与朋友一起使用已弃用的 API 并切换到Fragments.

于 2013-05-16T09:47:09.377 回答