Android:自定义标题栏导致应用程序崩溃
好的,我正在按照创建自定义标题栏的示例进行操作: http://thetransientway.com/?p= 100 。最终结果应该是这样的:
(来源:thetransientway.com)
问题是的,当我打开应用程序时,它就崩溃了。这就是我现在所拥有的:
活动文件:
package u.nav.it;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class UNavitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
}
custom_title.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:background="#005BAA"
android:layout_height="50dp">
<Button
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Button"
android:id="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp" >
</Button>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Title Content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/black" />
</RelativeLayout>
custom_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="u.nav.it"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CustomTitlebarActivityActivity" android:theme="@style/CustomTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Ok, I am following this example of creating a custom title bar: http://thetransientway.com/?p=100. the end result should be like this:
(source: thetransientway.com)
The problem is, the moment I open up the app, it just crashes. This is what I have right now:
Activity file:
package u.nav.it;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class UNavitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
}
custom_title.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:background="#005BAA"
android:layout_height="50dp">
<Button
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Button"
android:id="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp" >
</Button>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="Title Content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/black" />
</RelativeLayout>
custom_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="u.nav.it"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CustomTitlebarActivityActivity" android:theme="@style/CustomTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未在清单中定义活动
UNavitActivity
。将 CustomTitlebarActivityActivity 替换为 UNavitActivity,我希望它能正常工作。You haven't defined the activity
UNavitActivity
in your manifest. Replace CustomTitlebarActivityActivity with UNavitActivity and i hope it should work.正如已经说过的,你还没有定义你的活动
可能您更改了应用程序的名称,而不是遵循示例的名称,但您没有
在清单文件中
进行更改,将其编辑为
as already said you've not defined your activity
probably you changed the name of the app instead of following the example's one, but you didn't change
in the manifest file
edit it into