使用静态游标或变量时出错

时间:2013-04-30 06:34:43

标签: android eclipse findbugs

我开发了一个应用程序。然后我在我的应用程序代码中下载并运行eclipse上的find bug工具,我得到以下错误:

Bug: Write to static field 
com.abc.myapp.controllers.LoginScreenActivityController.loginDetailsCursor from instance method   
com.abc.myapp.controllers.LoginScreenActivityController.getLoginMasterData()

This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice. 

Confidence: High, Rank: Of Concern (15)
Pattern: ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD 
Type: ST, Category: STYLE (Dodgy code)

由于我的错误,我的代码如下:

public class LoginScreenActivityController extends Activity{
    public static Cursor loginDetailsCursor;
    DatabaseHelper database_helper;
    public LoginScreenActivityController() {
    }

    public ArrayList<Login_Master> getLoginMasterData(){
        database_helper = new DatabaseHelper(this);
        try{
            database_helper.openDataBase();
        } catch (Exception e){
            e.printStackTrace();
        }   

                /*
                *
                * **Getting **bug** here**
                *
                */
        loginDetailsCursor = database_helper.getLoginDetails();

        //Creating instance of class Login_Master model
        Login_Master login_master = new Login_Master();
        ArrayList<Login_Master> loginDetailsArrayList = new ArrayList<Login_Master>();
        if(loginDetailsCursor != null && loginDetailsCursor.getCount()>0 ){
            for(loginDetailsCursor.moveToFirst(); !loginDetailsCursor.isAfterLast(); loginDetailsCursor.moveToNext()){
                //Done Some thing .....
            }
        }else{
            //Done some thing ...
        }

        //Closing Data Base Connection.
        if (database_helper != null) 
            database_helper.close();

        return loginDetailsArrayList;       
    }

    public void updateLoginDetails(String username){
        try{
            database_helper.openDataBase();
        } catch (Exception e){
        e.printStackTrace();
        }

        database_helper.updateLoginTab(username);
        //Closing Data Base Connection.

                /*
                *
                *  **Getting **null check bug** here**
                *
                */
        if (database_helper != null) 
            database_helper.close();
    }   
}

我该怎么做才能消除这个错误。请建议我。

0 个答案:

没有答案