如何从android中的类调用片段?

时间:2016-03-29 11:58:23

标签: android

我有一个活动,其中一个按钮,当点击按钮我想在点击退出时调用片段,我该怎么做。它会抛出像...一样的错误...

这是我的代码

public class CLoginSessionManagement {
// User name (make variable public to access from outside)
public static final String s_szKEY_MOBILE = "agentCode";
// Email address (make variable public to access from outside)
public static final String s_szKEY_PASSWORD = "pin";
// Sharedpref file name
private static final String s_szPREF_NAME = "LoginData";
// All Shared Preferences Keys
private static final String s_szIS_LOGIN = "IsLoggedIn";
public SharedPreferences m_LoginPref;
public Editor m_Editor;
public Context m_Context;
public int n_PrivateMode = 0;

// Constructor
public CLoginSessionManagement(Context m_Context) {
    this.m_Context = m_Context;
    m_LoginPref = m_Context.getSharedPreferences(s_szPREF_NAME, n_PrivateMode);
    m_Editor = m_LoginPref.edit();
}

// Registeration Session Management....
public void setLoginData(String mobile, String pin) {
    m_Editor.putBoolean(s_szIS_LOGIN, true);
    m_Editor.putString(s_szKEY_MOBILE, mobile);
    m_Editor.putString(s_szKEY_PASSWORD, pin);
    m_Editor.commit();
}

/**
 * checkLogin() session wil check user Login status
 * If false it will redirect user to Login page
 * Else won't do anything
 */
public boolean checkLogin() {
    if (!isLogin()) {
        Intent i = new Intent(m_Context, CMainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        m_Context.startActivity(i);
        return true;
    }
    return false;
}

/**
 * Get stored Login session data
 */
public HashMap<String, String> getLoginDetails() {
    HashMap<String, String> user = new HashMap<>();
    // user name
    user.put(s_szKEY_MOBILE, m_LoginPref.getString(s_szKEY_MOBILE, null));
    // user email id
    user.put(s_szKEY_PASSWORD, m_LoginPref.getString(s_szKEY_PASSWORD, null));
    // return user
    return user;
}

public boolean isLogin() {
    return m_LoginPref.getBoolean(s_szIS_LOGIN, false);
}

/**
 * Clear session details
 */
public void logoutUser() {
    // Clearing all data from Shared Preferences
    m_Editor.clear();
    m_Editor.commit();
    Fragment m_oLoginScreen = new CLoginScreen();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container, m_oLoginScreen, null);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
}

在LogOut部分的getSupportfragmentManager中给我错误。 这是我的代码,点击事件

else if (id == R.id.LogOut) {
        s_oCloginSession.checkLogin();
        s_oCloginSession.logoutUser();
        Fragment m_oLoginScreen = new CLoginScreen();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, m_oLoginScreen, null);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

7 个答案:

答案 0 :(得分:1)

首先,您的类没有尝试调用getSupportFragmentManager的方法。

如果此类中的上下文是使用此方法扩展类的活动,则可以使用

((AppCompactActivity)m_Context).getSupportFragmentManager();

如果没有,您有两个选择,让您的活动类扩展AppCompactActivity或使用getFragmentManager。

答案 1 :(得分:0)

尝试使用getFragmentManager()或为您的Activity扩展AppCompatActivity类。

答案 2 :(得分:0)

您只能从活动扩展类(如AppcombatActvity或FragmentActivity)内部直接调用getSupportFragmnetManager。要从其他类获取支持片段管理器,请使用应用程序的上下文。替换

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

 FragmentTransaction fragmentTransaction = ((<activity_name>)m_Context).getSupportFragmentManager().beginTransaction();

答案 3 :(得分:0)

您无法访问 getSupportFragmentManager(),因为您的类没有扩展Activity。如果您想要访问非Activity类中的方法。传递上下文是一个Activity,你可以调用这样的方法:

final Activity activity = (Activity) context;

    activity.getSupportFragmentManager()

谢谢你,我希望这有用。

答案 4 :(得分:0)

我认为正确的方法是java类中的用户界面,并在您的活动中实现 这是使用接口的示例 https://www.dropbox.com/s/qirpdnd78xlbnzh/SamplePro_with_interface%27.zip?dl=0

答案 5 :(得分:0)

final Activity activity = (Activity) context;
             FragmentManager  mFragmentManager = (activity).getSupportFragmentManager();
    // if getSupportFragmentManager() doesnt work try with getFragmentManager and change the import of fragment
             FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                 fragmentTransaction.replace(R.id.containerView, new CLoginScreen());
                 fragmentTransaction.addToBackStack(null);
                 getSupportFragmentManager().popBackStack();
                 fragmentTransaction.commit();

答案 6 :(得分:0)

使用此:

public static void  t(Activity activity){
    LayoutInflater inflater = activity.getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast, null);
    TextView text = (TextView) layout.findViewById(R.id.toastId);

}