代码在onCreate中!当setContentView(R.layout.manual);
超出if
时,它会正常工作。但我将setContentView(R.layout.manual);
移至if
无效!
关注:
if (settings.getBoolean("my_first_time", true)) {
setContentView(R.layout.manual); // can not work
}
但Log.d("Comments1", "First time");
始终有效
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("my_first_time", true)) {
//the app is being launched for first time, do something
Log.d("Comments1", "First time");//this can be showed on logCat!
setContentView(R.layout.manual);// this can not work
// record the fact that the app has been started at least once
settings.edit().putBoolean("my_first_time", false).commit();
}
}
答案 0 :(得分:2)
你的病情没有得到满足,因为
settings.getBoolean("my_first_time", true)
未返回true。 因此你的
的setContentView(R.layout.manual)
未在“if”块内调用。
答案 1 :(得分:2)
如果设置if-else循环,则需要知道自己在做什么,因为无论结果如何,setContentView()
必须提供有效的布局ID。如果您在设置布局之前有条件要检查,则只需检查ID:
int layoutId=0;
if(condition) {
layoutId = R.layout.manual;
} else {
layoutId = R.layout.other;
}
setContentView(layoutId);