我是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);
}
logcat的:
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}:
答案 0 :(得分:2)
在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);
}
修改
该标题是Action Bar。
您可以通过编程方式隐藏标题和操作栏:
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>
答案 1 :(得分:0)
尝试使用样式:
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
</style>
在活动中,在super.onCreate()之前添加:
setTheme(R.style.MyTheme);
或者在android清单中添加android:theme property:
<activity android:name="YOUR_ACTIVITY" android:theme="@style/MyTheme"
(...)
</activity>