2013-05-08 1 views
-2

코드가 onCreate에 있습니다! setContentView(R.layout.manual);if 외부에있는 경우 작동합니다. 그러나 나는 setContentView(R.layout.manual);if로 움직일 수 없다!if 문에서 "setContentView"가 작동하지 않는 이유는 무엇입니까?

한 아래와 같은 :

if (settings.getBoolean("my_first_time", true)) { 
    setContentView(R.layout.manual); // can not work 
} 

그러나 Log.d("Comments1", "First time"); 항상

settings.getBoolean("my_first_time", true) 

사실이 반환되지 않기 때문에

@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

공공 무효에서 onCreate (번들 savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.manual); 조건이 거짓 일 수 있기 때문에 그렇게 사용하십시오. –

+0

전체 코드를 게시하십시오. –

답변

2

당신의 조건이 만족 못하고 있습니다 작동합니다. 따라서 귀하의

된 setContentView (R.layout.manual)

는 "만약"블록 내에서 호출되지 않습니다.

2

결과가 무엇이든 상관없이 올바른 레이아웃 ID가 제공되어야하므로 if-else 루프를 설정하면 무엇을하는지 알아야합니다. 당신이 레이아웃을 설정하기 전에 확인하는 조건이있는 경우, 당신은 단지 ID를 확인할 수 있습니다

int layoutId=0; 
if(condition) { 
    layoutId = R.layout.manual; 
} else { 
    layoutId = R.layout.other; 
} 
setContentView(layoutId); 
관련 문제