2

サンプルコード:

// activity contains different controls so inherits from Activity
public class Main extends Activity implements OnClickListener, TextWatcher  {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean titleSupported = false;            
        if (true) { // for on/off testing
          titleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        } 
        setContentView(R.layout.activity_main);

        // see: http://stackoverflow.com/questions/3438276/change-title-bar-text-in-android
        if (titleSupported)) {
          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
          final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
          if ( myTitleText != null ) {
            myTitleText.setText("@string/app_name");                    
          }                  
        }

「requestWindowFeature」を含めると、エミュレーターでアプリが停止/クラッシュします。理由がわかりません。私はEclipseとAndroidを初めて使用しますが、検索できるものから、正しい順序で物事を行っています。原因のアイデアはありますか?

「setContentView 」の前にrequestWindowFeature」 を使用すると、次のようになります。

02-04 12:35:05.883: E/AndroidRuntime(755): java.lang.RuntimeException: アクティビティ ComponentInfo{com.xxx.yyy/com.xxx.yyy.Main} を開始できません: android.util.AndroidRuntimeException: あなたカスタム タイトルを他のタイトル機能と組み合わせることはできません

「requestWindowFeature」の「setContentView」 を使用すると、次のようになります。

02-04 12:32:32.660: E/AndroidRuntime(784): java.lang.RuntimeException: アクティビティ ComponentInfo{com.xxx.yyy/com.xxx.yyy.Main} を開始できません: android.util.AndroidRuntimeException: requestFeature () は、コンテンツを追加する前に呼び出す必要があります

4

2 に答える 2

1

setcontentview の後に requestfeature 呼び出しを行う

    setContentView(R.layout.activity_main);
    titleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
于 2013-02-04T12:26:57.520 に答える