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 に答える