2012-10-18 55 views
0

我正在開發一個應用程序使用ABS庫。在兩個fragmentActivities中,我需要在操作欄中使用自定義視圖。我的代碼在ICS和JB中非常完美。但我在薑餅測試和墜毀。ActionBarSherlock getCustomView()返回null上的前ICS Android版本

我的代碼是下一個:

public class SearchContactActivity extends SherlockFragmentActivity { 

    private EditText searchField; 
    private ImageButton searchButton; 


    @Override 
    protected void onCreate(Bundle arg0) { 
     super.onCreate(arg0); 
     setContentView(R.layout.search_contacts); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     View view = actionBar.getCustomView();//ICS & JB return the View OK, lower versions returns null 
     searchField = (EditText) view.findViewById(R.id.editText_search);//here the app crash because view is null 
     searchButton = (ImageButton) view.findViewById(R.id.button_search); 

    } 
} 

我的主題XML:

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Theme.Styled.Search" parent="Theme.Sherlock.Light"> 
     <item name="actionBarStyle">@style/Widget.Styled.ActionBar.Search</item> 
     <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar.Search</item> 
    </style> 
    <style name="Widget.Styled.ActionBar.Search" parent="Widget.Sherlock.Light.ActionBar"> 
     <item name="background">@drawable/bg_actionbar</item> 
     <item name="android:background">@drawable/bg_actionbar</item> 
     <item name="backgroundSplit">@drawable/bg_actionbar</item> 
     <item name="android:backgroundSplit">@drawable/bg_actionbar</item> 
     <item name="android:titleTextStyle">@style/NoTitleText</item> 
     <item name="android:subtitleTextStyle">@style/NoTitleText</item> 
     <item name="android:displayOptions">showHome|useLogo|showCustom</item> 
     <item name="android:customNavigationLayout">@layout/custom_action</item> 
    </style> 
</resources> 

和我custom_view:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <EditText 
     android:id="@+id/editText_search" 
     style="@style/Widget.Styled.EditText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/button_search" 
     android:ems="10" 
     android:hint="@string/hint_search" 
     android:inputType="textNoSuggestions" 
     android:layout_marginRight="5dp" /> 

    <ImageButton 
     android:id="@+id/button_search" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/editText_search" 
     android:layout_alignTop="@+id/editText_search" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:background="@drawable/button_go_login_xml" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:src="@drawable/lupa" /> 

</RelativeLayout> 

我logcat的

10-18 18:03:34.259: E/AndroidRuntime(10576): FATAL EXCEPTION: main 
10-18 18:03:34.259: E/AndroidRuntime(10576): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.co2fine.client/com.co2fine.client.SearchContactActivity}: java.lang.NullPointerException 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.os.Looper.loop(Looper.java:123) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.main(ActivityThread.java:3729) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at java.lang.reflect.Method.invokeNative(Native Method) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at java.lang.reflect.Method.invoke(Method.java:507) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at dalvik.system.NativeStart.main(Native Method) 
10-18 18:03:34.259: E/AndroidRuntime(10576): Caused by: java.lang.NullPointerException 
10-18 18:03:34.259: E/AndroidRuntime(10576): at com.co2fine.client.SearchContactActivity.onCreate(SearchContactActivity.java:53) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-18 18:03:34.259: E/AndroidRuntime(10576): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
10-18 18:03:34.259: E/AndroidRuntime(10576): ... 11 more 

回答

5

嘗試增加:

<item name="customNavigationLayout">@layout/custom_action</item> 

與附和:

<item name="android:customNavigationLayout">@layout/custom_action</item> 
+0

謝謝你,它的工作我! – ClarkXP

相關問題