1

我正在使用 ActionBarSherlock 和自定义样式:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:textColor="@color/grey"
            android:id="@+id/bar_title"
            style="@style/Text.Medium">
    </TextView>

</LinearLayout>
View mActionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom, null);
getSupportActionBar().setCustomView(mActionBarView);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_HOME);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

在我的活动中,我重写了 setTitle 方法:

@Override
public void setTitle(CharSequence title) {
    TextView txt = (TextView) findViewById(R.id.bar_title);
    txt.setText(title);
}

但是当我在片段中调用 getActivity().setTitle() 时,覆盖的 setTitle 中的变量 txt 等于 null。有什么问题?

日志:

09-06 18:18:21.807: ERROR/AndroidRuntime(953): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alwx.reader/com.alwx.reader.activity.BookActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
        at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
        at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:251)
        at com.alwx.reader.activity.BookActivity.onCreate(BookActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        ... 11 more
        Caused by: java.lang.NullPointerException
        at com.alwx.reader.activity.BookActivity.setTitle(BookActivity.java:28)
        at com.alwx.reader.fragment.BookFragment.onCreateView(BookFragment.java:52)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
        at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
        ... 22 more
4

1 回答 1

2

//这会起作用

 TextView txt = (TextView)mActionBarView. findViewById(R.id.bar_title);
    txt.setText(title);

//没有Text.Medium的默认样式。你被创造了改变它

style="@android:style/TextAppearance.Medium"
于 2012-09-06T14:08:04.057 回答