0

我正在使用 ASYNTask。而且我使用了自定义进度条。它在旋转一段时间后停止工作,然后出现一个空白(黑色)屏幕 2 秒,然后我的下一个屏幕的 UI 加载。

public class LoginActivity extends Activity {
    /** Variable define here. */
    private EditText metLoginUserName, metLoginPassword;
    private Button mbtnLogin;
    private ImageView ivRegister;
    private String Host, username, password;
    private int Port;
    private UserChatActivity xmppClient;
    public static ArrayList<String> all_user = new ArrayList<String>();
    public static CustomProgressDialog mCustomProgressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginlayout);

        metLoginUserName = (EditText) this.findViewById(R.id.etLoginUserName);
        metLoginPassword = (EditText) this.findViewById(R.id.etLoginPassword);
        mbtnLogin = (Button) findViewById(R.id.btnLogin);
        ivRegister = (ImageView) findViewById(R.id.ivRegister);

        /** Set the hint in username and password edittext */
        metLoginUserName = CCMStaticMethod.setHintEditText(metLoginUserName,
                getString(R.string.hint_username), true);
        metLoginPassword = CCMStaticMethod.setHintEditText(metLoginPassword,
                getString(R.string.hint_password), true);

        /** Click on login button */
        mbtnLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                /** Login Activity */
                username = metLoginUserName.getText().toString();
                password = metLoginPassword.getText().toString();
                CCMStaticVariable.musername = username;
                CCMStaticVariable.musername = password;
                if(CCMStaticMethod.isInternetAvailable(LoginActivity.this)){
                    LoginUserTask loginUserTask = new LoginUserTask(v.getContext());
                    loginUserTask.execute();
                }
            }
        });

        /** Click on forgot button */
        this.findViewById(R.id.ivForgot).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(LoginActivity.this,
                                ForgotPassActivity.class));
                    }
                });

        /** Click on register button */
        ivRegister.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,
                        RegisterActivity.class));
            }
        });
    }

    public class LoginUserTask extends AsyncTask<Void, Void, XMPPConnection> {
        public LoginUserTask(Context context) {
            super();
            this.context = context;
        }
        private Context context;

        @Override
        protected void onPostExecute(XMPPConnection result) {
            if (result != null) {
                /**Start services*/
                startService(new Intent(LoginActivity.this, UpdaterService.class));
                /**Call usermenu activity*/ 
                finish();
                startActivity(new Intent(LoginActivity.this,UserMenuActivity.class));
            } else {
                DialogInterface.OnClickListener LoginUnSuccessOkAlertListener = new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                };
                CCMStaticMethod.showAlert(context, "Login",
                        "Invalid Username or Password. Try Again !", R.drawable.unsuccess,
                        true, true, "Ok", LoginUnSuccessOkAlertListener, null,
                        null);
            }
            mCustomProgressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            mCustomProgressDialog = CustomProgressDialog.createDialog(
                    LoginActivity.this, "", "");
            mCustomProgressDialog.show();
        }

        @Override
        protected XMPPConnection doInBackground(Void... params) {
            CCMStaticVariable.CommonConnection = XMPPConn.checkXMPPConnection(username,
                    password);
            return CCMStaticVariable.CommonConnection;
        }
    }
}

用户菜单活动

public class UserMenuActivity extends ExpandableListActivity {

    private XMPPConnection connection;
    String name,availability,subscriptionStatus;
    TextView tv_Status;
    public static final String ACTION_UPDATE = "ACTION_UPDATE";

    /** Variable Define here */
    private String[] data = { "View my profile", "New Multiperson Chat",
            "New Broad Cast Message", "New Contact Category", "New Group",
            "Invite to CCM", "Search", "Expand All", "Settings", "Help",
    "Close" };
    private String[] data_Contact = { "Rename Category","Move Contact to Category", "View my profile",
            "New Multiperson Chat", "New Broad Cast Message",
            "New Contact Category", "New Group", "Invite to CCM", "Search",
            "Expand All", "Settings", "Help", "Close" };

    private String[] data_child_contact = { "Open chat", "Delete Contact","View my profile",
            "New Multiperson Chat", "New Broad Cast Message",
            "New Contact Category", "New Group", "Invite to CCM", "Search",
            "Expand All", "Settings", "Help", "Close" };
    private String[] menuItem = { "Chats", "Contacts", "CGM Groups", "Pending","Request" };
    private List<String> menuItemList = Arrays.asList(menuItem);

    private int commonGroupPosition = 0;
    private String etAlertVal;
    private DatabaseHelper dbHelper;
    private int categoryID, listPos;
    /** New Code here.. */
    private ArrayList<String> groupNames;
    private ArrayList<ArrayList<ChildItems>> childs;
    private UserMenuAdapter adapter;
    private Object object;
    private CustomProgressDialog mCustomProgressDialog;

    private String[] data2 = { "PIN Michelle", "IP Call" };
    private ListView mlist2;
    private ImageButton mimBtnMenu;
    private LinearLayout mllpopmenu;
    private View popupView;
    private PopupWindow popupWindow;
    private AlertDialog.Builder alert;
    private EditText input;
    private TextView mtvUserName, mtvUserTagLine;
    private ExpandableListView mExpandableListView;
    public static List<CategoryDataClass> categoryList;
    public static ArrayList<String> chatUser;
    private boolean menuType = false;
    private String childValContact="";
    public static Context context;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.usermenulayout);
        dbHelper = new DatabaseHelper(UserMenuActivity.this);
        XMPPConn.getContactList();
        connection = CCMStaticVariable.CommonConnection;
        registerReceiver(UpdateList, new IntentFilter(ACTION_UPDATE));

    }

    @Override
    public void onBackPressed() {
        if (mllpopmenu.getVisibility() == View.VISIBLE) {
            mllpopmenu.setVisibility(View.INVISIBLE);
        } else {
            if (CCMStaticVariable.CommonConnection.isConnected()) {
                CCMStaticVariable.CommonConnection.disconnect();
            }
            super.onBackPressed();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            if (mllpopmenu.getVisibility() == View.VISIBLE) {
                mllpopmenu.setVisibility(View.INVISIBLE);
            } else {
                if (commonGroupPosition >= 4 && menuType == true) {
                    if(childValContact == ""){
                        mllpopmenu.setVisibility(View.VISIBLE);

                        mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                                R.layout.listviewtext, R.id.tvMenuText,
                                data_Contact));

                    }else{
                        mllpopmenu.setVisibility(View.VISIBLE);
                        mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                                R.layout.listviewtext, R.id.tvMenuText,
                                data_child_contact));

                    }

                } else if (commonGroupPosition == 0) {
                    mllpopmenu.setVisibility(View.VISIBLE);
                    mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
                            R.layout.listviewtext, R.id.tvMenuText, data));
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



    @Override
    protected void onResume() {
        super.onResume();
        mCustomProgressDialog = CustomProgressDialog.createDialog(
                UserMenuActivity.this, "", "");
        mCustomProgressDialog.show();
        new Thread(){
            public void run() {
                XMPPConn.getContactList();
                expendableHandle.sendEmptyMessage(0);
            };
        }.start();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        unregisterReceiver(UpdateList);
    }

    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        if(groupPosition == 0){
            Intent intent = new Intent(UserMenuActivity.this,UserChatActivity.class);
            intent.putExtra("userNameVal",chatUser.get(childPosition));
            intent.putExtra("message","");          
            startActivity(intent);
        }else if (groupPosition == 1 && childPosition == 0) {
            startActivity(new Intent(UserMenuActivity.this,
                    InvitetoCCMActivity.class));
        } else if (groupPosition == 1 && childPosition != 0) {
            Intent intent = new Intent(UserMenuActivity.this,
                    UserChatActivity.class);
            intent.putExtra("userNameVal",
                    XMPPConn.mfriendList.get(childPosition - 1).friendName);
            intent.putExtra("message","");
            startActivity(intent);
        } else if (groupPosition == 2 && childPosition == 0) {
            startActivity(new Intent(UserMenuActivity.this,
                    CreateGroupActivity.class));
        } else if (groupPosition == 2 && childPosition != 0) {
            String GROUP_NAME = childs.get(groupPosition).get(childPosition)
            .getName().toString();
            int end = GROUP_NAME.indexOf("(");
            CCMStaticVariable.groupName = GROUP_NAME.substring(0, end).trim();
            startActivity(new Intent(UserMenuActivity.this,
                    GroupsActivity.class));
        } else if (groupPosition >= 4) {
            childValContact = childs.get(groupPosition).get(childPosition).getName().trim();
            showToast("user==>"+childValContact, 0);
        }
        return false;
    }

    private void setExpandableListView() {
        /***###############GROUP ARRAY ############################*/
        final ArrayList<String> groupNames = new ArrayList<String>();
        chatUser = dbHelper.getChatUser();
        groupNames.add("Chats ("+chatUser.size()+")");  
        groupNames.add("Contacts (" + XMPPConn.mfriendList.size() + ")");
        groupNames.add("CGM Groups (" + XMPPConn.mGroupList.size() + ")");
        groupNames.add("Pending (1)");

        XMPPConn.getGroup();
        categoryList = dbHelper.getAllCategory();
        /**Group From Sever*/
        if (XMPPConn.mGroupList.size() > 0) {
            for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
                XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                groupNames.add(XMPPConn.mGroupList.get(g).groupName + "("
                        + XMPPConn.mGroupContactList.size()+ ")");
            }
        } 
        if(categoryList.size() > 0){
            for (int cat = 0; cat < categoryList.size(); cat++) {
                groupNames.add(categoryList.get(cat).getCategoryName()+ "(0)");
            }
        }
        this.groupNames = groupNames;

        /*** ###########CHILD ARRAY * #################*/
        ArrayList<ArrayList<ChildItems>> childs = new ArrayList<ArrayList<ChildItems>>();
        ArrayList<ChildItems> child = new ArrayList<ChildItems>();
        if(chatUser.size() > 0){
            for(int i = 0; i < chatUser.size(); i++){
                child.add(new ChildItems(userName(chatUser.get(i)), "",0,null));
            }
        }else{
            child.add(new ChildItems("--No History--", "",0,null));
        }       
        childs.add(child);

        child = new ArrayList<ChildItems>();
        child.add(new ChildItems("", "",0,null));
        if (XMPPConn.mfriendList.size() > 0) {
            for (int n = 0; n < XMPPConn.mfriendList.size(); n++) {
                child.add(new ChildItems(XMPPConn.mfriendList.get(n).friendNickName,
                        XMPPConn.mfriendList.get(n).friendStatus, 
                        XMPPConn.mfriendList.get(n).friendState,
                        XMPPConn.mfriendList.get(n).friendPic));
            }
        }
        childs.add(child);
        /************** CGM Group Child here *********************/
        child = new ArrayList<ChildItems>();
        child.add(new ChildItems("", "",0,null));

        if (XMPPConn.mGroupList.size() > 0) {
            for (int grop = 0; grop < XMPPConn.mGroupList.size(); grop++) {
                child.add(new ChildItems(
                        XMPPConn.mGroupList.get(grop).groupName + " ("
                        + XMPPConn.mGroupList.get(grop).groupUserCount
                        + ")", "",0,null));
            }
        }
        childs.add(child);

        child = new ArrayList<ChildItems>();
        child.add(new ChildItems("Shuchi",
                "Pending (Waiting for Authorization)",0,null));
        childs.add(child);
        /************************ Group Contact List *************************/
        if (XMPPConn.mGroupList.size() > 0) {
            for (int g = 0; g < XMPPConn.mGroupList.size(); g++) {
                /** Contact List */
                XMPPConn.getGroupContact(XMPPConn.mGroupList.get(g).groupName);
                child = new ArrayList<ChildItems>();
                for (int con = 0; con < XMPPConn.mGroupContactList.size(); con++) {
                    child.add(new ChildItems(
                            XMPPConn.mGroupContactList.get(con).friendName,
                            XMPPConn.mGroupContactList.get(con).friendStatus,0,null));
                }
                childs.add(child);
            }
        } 
        if(categoryList.size() > 0){
            for (int cat = 0; cat < categoryList.size(); cat++) {
                child = new ArrayList<ChildItems>();
                child.add(new ChildItems("-none-", "",0,null));
                childs.add(child);
            }
        }
        this.childs = childs;

        /** Set Adapter here */
        adapter = new UserMenuAdapter(this, groupNames, childs);
        setListAdapter(adapter);
        object = this;

        mlist2 = (ListView) findViewById(R.id.list2);
        mimBtnMenu = (ImageButton) findViewById(R.id.imBtnMenu);
        mllpopmenu = (LinearLayout) findViewById(R.id.llpopmenu);
        mtvUserName = (TextView) findViewById(R.id.tvUserName);
        mtvUserTagLine = (TextView) findViewById(R.id.tvUserTagLine);

        //Set User name..
        System.out.println("CCMStaticVariable.loginUserName==="
                + CCMStaticVariable.loginUserName);
        if (!CCMStaticVariable.loginUserName.equalsIgnoreCase("")) {
            mtvUserName.setText("" + CCMStaticVariable.loginUserName);
        }
        /** Expandable List set here.. */
        mExpandableListView = (ExpandableListView) this
        .findViewById(android.R.id.list);
        mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {   

                if (parent.isGroupExpanded(groupPosition)) {
                    commonGroupPosition = 0;
                }else{
                    commonGroupPosition = groupPosition;
                }
                String GROUP_NAME = groupNames.get(groupPosition);
                int end = groupNames.get(groupPosition).indexOf("(");
                String GROUP_NAME_VALUE = GROUP_NAME.substring(0, end).trim();
                if (menuItemList.contains(GROUP_NAME_VALUE)) {
                    menuType = false;
                    CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
                } else {
                    menuType = true;
                    CCMStaticVariable.groupCatName = GROUP_NAME_VALUE;
                }
                long findCatId = dbHelper.getCategoryID(GROUP_NAME_VALUE);
                if (findCatId != 0) {
                    categoryID = (int) findCatId;
                }
                childValContact="";
                //showToast("Clicked on==" + GROUP_NAME_VALUE, 0);
                return false;
            }
        });

        /** Click on item */
        mlist2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long arg3) {
                if (commonGroupPosition >= 4) {
                    if(childValContact == ""){
                        if (pos == 0) {
                            showAlertEdit(CCMStaticVariable.groupCatName);
                        }                   
                        /** Move contact to catgory */
                        if (pos == 1) {
                            startActivity(new Intent(UserMenuActivity.this,AddContactCategoryActivity.class));
                        }
                    }else{
                        if(pos == 0){
                            Intent intent = new Intent(UserMenuActivity.this,UserChatActivity.class);
                            intent.putExtra("userNameVal",childValContact);
                            startActivity(intent);
                        }
                        if(pos == 1){
                            XMPPConn.removeEntry(childValContact);
                            showToast("Contact deleted sucessfully", 0);
                            Intent intent = new Intent(UserMenuActivity.this,UserMenuActivity.class);
                        }
                    }

                } else {
                    /** MyProfile */
                    if (pos == 0) {
                        startActivity(new Intent(UserMenuActivity.this,
                                MyProfileActivity.class));
                    }
                    /** New multiperson chat start */
                    if (pos == 1) {
                        startActivity(new Intent(UserMenuActivity.this,
                                NewMultipersonChatActivity.class));
                    }
                    /** New Broadcast message */
                    if (pos == 2) {
                        startActivity(new Intent(UserMenuActivity.this,
                                NewBroadcastMessageActivity.class));
                    }
                    /** Click on add category */
                    if (pos == 3) {
                        showAlertAdd();
                    }
                    if (pos == 4) {
                        startActivity(new Intent(UserMenuActivity.this,
                                CreateGroupActivity.class));
                    }

                    if (pos == 5) {
                        startActivity(new Intent(UserMenuActivity.this,
                                InvitetoCCMActivity.class));
                    }
                    if (pos == 6) {
                        startActivity(new Intent(UserMenuActivity.this,
                                SearchActivity.class));
                    }
                    if (pos == 7) {
                        onGroupExpand(2);
                        for (int i = 0; i < groupNames.size(); i++) {
                            mExpandableListView.expandGroup(i);
                        }
                    }
                    /** Click on settings */
                    if (pos == 8) {
                        startActivity(new Intent(UserMenuActivity.this,
                                SettingsActivity.class));
                    }
                    if (pos == 10) {
                        System.exit(0);
                    }

                    if (pos == 14) {
                        if (mllpopmenu.getVisibility() == View.VISIBLE) {
                            mllpopmenu.setVisibility(View.INVISIBLE);
                            if (popupWindow.isShowing()) {
                                popupWindow.dismiss();
                            }
                        } else {
                            mllpopmenu.setVisibility(View.VISIBLE);


                            mlist2.setAdapter(new ArrayAdapter(
                                    UserMenuActivity.this,
                                    R.layout.listviewtext, R.id.tvMenuText,
                                    data));
                        }
                    }
                }
            }
        });
    }

    /** Toast message display here.. */
    private void showToast(String msg, int time) {
        Toast.makeText(this, msg, time).show();
    }

    /** Show EditAlert Box */
    private void showAlertEdit(String msg) {
        alert = new AlertDialog.Builder(UserMenuActivity.this);
        input = new EditText(UserMenuActivity.this);
        input.setSingleLine();
        input.setText(msg);
        alert.setTitle("Edit Category Name");
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
                etAlertVal = input.getText().toString();
                /** isGroupFromServerOrMobile */
                RosterGroup isGroupExists = CCMStaticVariable.CommonConnection
                .getRoster().getGroup(CCMStaticVariable.groupCatName);
                if (isGroupExists == null) {
                    dbHelper.updateCategory(categoryID, etAlertVal);
                    Intent intent = new Intent(UserMenuActivity.this,
                            UserMenuActivity.class);
                    startActivity(intent);
                    showToast("Category updated sucessfully", 0);
                } else {
                    CCMStaticVariable.CommonConnection.getRoster()
                    .getGroup(CCMStaticVariable.groupCatName)
                    .setName(etAlertVal);
                    Intent intent = new Intent(UserMenuActivity.this,
                            UserMenuActivity.class);
                    startActivity(intent);
                    showToast("Category updated sucessfully", 0);
                }
            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        alert.show();
    }

    /** ShowAlertBox Edit */
    private void showAlertAdd() {
        alert = new AlertDialog.Builder(UserMenuActivity.this);
        input = new EditText(UserMenuActivity.this);
        input.setSingleLine();
        alert.setTitle("New Category Name");
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
                etAlertVal = input.getText().toString();
                if (!etAlertVal.equalsIgnoreCase("request")) {
                    long lastInsertedId = dbHelper.insertCategory(etAlertVal);
                    CCMStaticVariable.groupCatName = etAlertVal;
                    Intent intent = new Intent(UserMenuActivity.this,AddContactCategoryActivity.class);                 
                    startActivity(intent);
                }
            }
        });
        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        alert.show();
    }



    public String showSubscriptionStatus(String friend){
        return friend;
    }
    public String userName(String jid){
        String username = "";
        if(jid.contains("@")){
            int index = jid.indexOf("@");
            username = jid.substring(0, index);
        }else{
            username = jid;
        }       
        return username;
    }

    /**Set expandable handler here..*/
    Handler expendableHandle = new Handler(){
        public void handleMessage(android.os.Message msg) {
            mCustomProgressDialog.dismiss();
            setExpandableListView();
        };
    };

    BroadcastReceiver UpdateList = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Toast.makeText(UserMenuActivity.this, "CALLED", Toast.LENGTH_SHORT).show();
            adapter.notifyDataSetChanged();


        }
    };


}

请建议我这里有什么问题。我看过很多帖子,并尝试了很多关于相同的事情。但它对我不起作用。请告诉我。

谢谢

4

1 回答 1

1

这个

        startActivity(new Intent(LoginActivity.this,UserMenuActivity.class));

这个

CCMStaticMethod.showAlert(context, "Login",
                        "Invalid Username or Password. Try Again !", R.drawable.unsuccess,
                        true, true, "Ok", LoginUnSuccessOkAlertListener, null,
                        null);
            }

和这个

    mCustomProgressDialog.dismiss();

不要从异步任务执行 UI 事件。改为使用activity.runOnUiThread()

于 2012-10-20T11:29:39.503 回答