我已经为设置活动定义了扩展 Dialogpreference 类的自定义首选项类,
public class YesNoPreference extends DialogPreference {
private boolean mWasPositiveResult;
DashboardActivity dashboardActivity;
Context prefContext;
public YesNoPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
userSession = new UserSessions(context);
prefContext = context;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (callChangeListener(positiveResult)) {
setValue(positiveResult);
/* Unset all user shared preferences */
userSession.unsetSessionData();
try {
dashboardActivity = new DashboardActivity();
//dashboardActivity.loginScreen();
Intent dashboard = new Intent(prefContext, DashboardActivity.class);
dashboardActivity.startActivity(dashboard);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Sets the value of this preference, and saves it to the persistent store
* if required.
*
* @param value The value of the preference.
*/
public void setValue(boolean value) {
mWasPositiveResult = value;
persistBoolean(value);
notifyDependencyChange(!value);
}
我正在使用对话框首选项从应用程序中注销用户。因此,如果用户选择“确定”,则将取消设置共享首选项,然后将用户定向到登录页面。
我尝试在 Activity 类中创建一个函数,然后在此类中调用它。也使用了 Intent 类,但执行停止在
dashboardActivity.startActivity(dashboard);
并生成空指针异常。
请帮助我,找到解决方案。
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
}
}