2013-02-23 61 views
0

我正在創建一個簡單的Android應用程序,我想向其添加自定義標題欄。不過,當我創建自定義標題欄時,我遇到了一個問題。Android設置自定義標題欄錯誤

這是我在GetPhoto Activity類的OnCreate方法:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     boolean titled = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE 
     setContentView(R.layout.getphoto); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_complex); 

     initialize(); 
    } 

而且我title_complex.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="35dip" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 

    <Imageview 
     android:src="@drawable/header" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:text="Hello world!" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#7cfc00" /> 

</LinearLayout> 

當我把getWindow()setFeatureInt(。 )之後setContentView(),我得到這樣的錯誤:

FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.featureselection/com.example.featureselection.GetPhoto}: android.view.InflateException: Binary XML file line #16: Error inflating class Imageview 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 
at android.app.ActivityThread.access$600(ActivityThread.java:127) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
... 

featureselection是應用程序名稱GetPhoto是包含上面顯示的OnCreate方法的類。 我不知道爲什麼它會給出錯誤。我看到其他人也寫這樣的代碼,它工作正常。如果我在setContentView()之前放置getWindow()。setFeatureInt(),當我運行我的應用程序時,它不會給出錯誤,但是自定義標題欄上沒有顯示任何內容,而我希望圖像和「hello world」在自定義標題欄上顯示。

FYI: 我也有這個customer_style.xml在res /值文件夾:

<resources> 
    <style name="CustomWindowTitleBackground"> 
     <item name="android:background">#008000</item> 
    </style> 

    <style name="CustomTheme" parent="android:Theme"> 
     <item name="android:windowTitleSize">35dip</item> 
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> 
    </style> 
</resources> 

設置樣式。

是否有人知道問題是什麼以及如何解決問題?任何細分或解決方案都將被處理。

回答

0

確保您的XML文件使用的是ImageView而不是Imageview - 元素名稱區分大小寫。

+0

謝謝,實際上是這個問題....它讓我感到很愚蠢..... – user1642888 2013-02-24 12:21:09