2012-09-06 74 views
1

我使用ActionBarSherlock和個性化風格吧:findViewById在活動問題

<?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 

回答

2

//這將工作

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

//沒有默認風格Text.Medium。你被創造了它改變它

style="@android:style/TextAppearance.Medium" 
+0

不,它不工作。 NullPointerException – alwx

+0

發佈錯誤日誌。 –

+0

完成。有任何想法嗎? – alwx