2017-01-11 74 views
2

我不問了解決方案,但想知道爲什麼TextView內部工具欄返回null? 我在片段內使用Butterknife。TextView的工具欄空與Butterknife

@BindView(R2.id.toolbarTitle) 
    TextView toolbarTitle; 

這裏是我的佈局:

<android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar_widget" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorAccent" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       > 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:gravity="center"> 

        <TextView 
         android:id="@+id/toolbarTitle" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:gravity="center" 
         android:textColor="#ffffff" 
         android:textSize="@dimen/text_size18" 
         android:textStyle="bold" /> 
       </LinearLayout> 
      </android.support.v7.widget.Toolbar> 
     </android.support.design.widget.AppBarLayout> 

這裏是我的logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 

好這裏的要求代碼:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.fragment_nav_bookmark_trend_my_quote, container, false); 
     toolbarTitle.setText(getString(R.string.bookmarks)); 
     accessToken = AppSharedPreferences.getsharedprefInstance(getContext()).getStringValue(AppSharedPreferences.KEY_USER_TOKEN); 
     mUnbinder = ButterKnife.bind(this, view); 
     mLinearLayout = new LinearLayoutManager(getActivity()); 
     mRecyclerView.setLayoutManager(mLinearLayout); 


     DrawerLayout drawer = ((EpicSwiperActivity) getActivity()).drawerLayout; 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       getActivity(), drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     addRecyclerViewScrollState(mRecyclerView); 
     return view; 
} 
+0

郵政編碼的片段你在哪裏使用textview –

+0

什麼是R2的意思? –

+0

用途:'toolbarTitle.setText(R.string.your_title);' –

回答

0

剛剛嘗試這一點

Toolbar toolbar = (Toolbar) findViewById(toolbar_widget); 
getActivity().setSupportActionBar(toolbar); 

TextView tvTitle = (TextView) toolbar.findViewById(R.id.toolbarTitle); 
tvTitle.setText(title); 

通過只是試試這個

TextView textView = ButterKnife.findById(toolbar, R.id.toolbarTitle); 

{{編輯}}

toolbar.xml

<android.support.v7.widget.Toolbar 
    android:background="?attr/colorAccent" 
    android:id="@+id/toolbar_widget" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_width="match_parent" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <LinearLayout 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"> 

     <TextView 
      android:gravity="center" 
      android:id="@+id/toolbarTitle" 
      android:layout_gravity="center" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:textColor="#ffffff" 
      android:textSize="@dimen/text_size18" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

fragmentlayout.xml

<android.support.design.widget.AppBarLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"> 

    <include 
     android:id="@+id/toolbar_widget" 
     layout="@layout/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</android.support.design.widget.AppBarLayout> 

Java文件

View toolbarView = getLayoutInflater().inflate(R.layout.toolbar,null); 
TextView textView = ButterKnife.findById(toolbarView, R.id.toolbarTitle); 
+0

既然我們不能用setSupportActionBar(工具欄),我已經使用'((AppCompatActivity)getActivity())setSupportActionBar(工具欄)。 &((MyActivity)getActivity())。setSupportActionBar(toolbar);'但它沒有工作。 – Debdeep

+0

好吧,我以爲你在調用你的活動,我會編輯它。 –

+0

但它沒有奏效。只有工作沒有使用Butterknife並引用'toolbar.findViewById(R.id.toolbarTitle)'。 – Debdeep

0

你有沒有設置你的toolbarActionBar正確,否則做得一樣:

ToolBar toolbar = (Toolbar) findViewById(R.id.toolbar_widget); 
setSupportActionBar(toolbar); 
1

橫指與你@BindView使用的ID您的TextView的ID。

而且,由於您使用的片段確保您在片段次方根觀點與傳遞,當你調用上下文一起ButterKnife.bind(this, rootView);

例如

View view = inflater.inflate(R.layout.fancy_fragment, container, false); 
    ButterKnife.bind(this, view);