我需要将LoginMechanism更改为asynchTask,以便它不会在SDK 4.x中引发android.os.NetworkOnMainThreadException。我不清楚如何在此异步任务中启动新的意图。我了解该工作将在doInBackground-Method中完成,并且此返回结果将在onPostExecute方法中进行处理。在这种方法中,我尝试启动新活动和服务,但它向我显示了编译器错误

The constructor Intent(MainActivity.LoginTask, Class<AttachService>) is undefined


这是onPostExecute方法

        @Override
    protected void onPostExecute(String result) {
        super.onPostExecute( result );
        if (result.equals("")){
            loginError.setText(R.string.login_error);
            loginError.setVisibility(View.VISIBLE);
        }
        else{
            loginError.setVisibility(View.INVISIBLE);
            startService(new Intent(LoginTask.this, AttachService.class));
            Log.d(TAG, "setting status of user " + login.getText().toString() + " to stored for service endpoint  " + service_endpoint_spinner.getSelectedItemId());
            if (appData.getLoggedInUser() == null){
                loggedInUser = DBManager.getInstance().storeUser(user,hashedPw,target, true);
                appData.setLoggedInUser(loggedInUser);
                DBManager.getInstance().setUserStatusToStored(loggedInUser);
            }
            startActivity(new Intent(this, ActionActivity.class));
        }

    }


所有的startservice和startActivity方法都会产生编译器错误,有人可以解释原因。
谢谢

最佳答案

采用

startActivity(new Intent(Your_Current_Activity.this, ActionActivity.class));


要么

startActivity(new Intent(getApplicationContext(), ActionActivity.class));


代替

startActivity(new Intent(this, ActionActivity.class));


用于从onPostExecute开始新活动

关于android - 是否可以在onPostExecute中启动 Activity ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13936077/

10-12 03:53