我是一名Android初学者,我写了一个非常简单的项目。一切都好。但是当我写requestWindowFeature(Window.FEATURE_NO_TITLE);时,项目运行失败。 IDE告诉我,我是在setContentView之后写的,但实际上我是在setContentView之前写的。我尝试单击“清理项目”并“重新启动IDE”,但是仍然会发生。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
}


日志猫:

02-25 18:09:48.447    2908-2908/com.wecan.wecanclub D/AndroidRuntime﹕ Shutting down VM
02-25 18:09:48.451    2908-2908/com.wecan.wecanclub E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wecan.wecanclub, PID: 2908
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wecan.wecanclub/com.wecan.wecanclub.MainActivity}:

最佳答案

requestWindowFeature(Window.FEATURE_NO_TITLE);移到super.onCreate();之前

protected void onCreate(Bundle savedInstanceState) {
     requestWindowFeature(Window.FEATURE_NO_TITLE);

     super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_main);
}


编辑

该标题是动作栏。

您可以通过编程方式隐藏标题和操作栏:

public class MainActivity extends Activity {

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getActionBar();
        if (actionBar != null) {

            actionBar.hide();
        }
    }
}


您也可以使用样式:

styles.xml:

 <resources>

     <style name="AppTheme" parent="android:Theme.Holo.Light">
          <item name="android:windowActionBar">false</item>
          <item name="android:windowNoTitle">true</item>
     </style>

 </resources>


AndroidManifest.xml中的活动:



    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

关于android - 添加requestWindowFeature()时无法启动 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28726348/

10-10 18:43