0

我正在使用 ABS 库开发应用程序。在两个 fragmentActivities 中,我需要在操作栏中使用自定义视图。我的代码在 ICS 和 JB 中完美运行。但是我在姜饼中测试并崩溃了。

我的代码是下一个:

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>

我的日志猫

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
4

1 回答 1

5

尝试添加:

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

一起去:

<item name="android:customNavigationLayout">@layout/custom_action</item>
于 2012-10-18T22:10:40.777 回答