public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment(),"root")
.commit();
}
}
@Override
protected void onStart() {
super.onStart();
Fragment RootFragment = getSupportFragmentManager().findFragmentByTag("root");
assert RootFragment != null;
View root_view = RootFragment.getView();
assert root_view != null;
CheckBox checkBox_have_authorized = findViewById(R.id.have_authorized);
if (checkBox_have_authorized!= null)
checkBox_have_authorized.setOnCheckedChangeListener(new CbxHaveAuthorizedListener());
else
Log.e("find error","checkBox_have_authorized is null");
}
class CbxHaveAuthorizedListener implements CompoundButton.OnCheckedChangeListener
{
@Override
public void onCheckedChanged(CompoundButton button,boolean checked) {
Toast.makeText(getApplicationContext(), "按钮被点击了", Toast.LENGTH_SHORT).show();
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
请问checkbox一直是空是为什么?