2016-11-24 3 views
-1

ツールバーのレイアウト内の要素にアクセスしたいが、nullになります。 このTextViewにアクセスするには何が必要ですか?ツールバーのレイアウト内のTextViewにアクセスするときにnullになるのはなぜですか?

そして、すべてのクラスのツールバーを整理するのが正しい方法ですか?

これは、私はTextViewのにアクセスしたい場所です私のmain_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.myapp.activity.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:background="?attr/colorBackgroundFloating" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:contentInsetStart="0dp" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <TextView 
       android:id="@+id/txtToolbarTitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:text="myapp" 
       android:textColor="@color/black" 
       android:textSize="20sp" 
       android:textStyle="bold" /> 

      <ImageButton 
       android:id="@+id/imbToolbarAdd" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="5dp" 
       android:background="@color/white" 
       android:src="@drawable/plus_circle" 
       android:visibility="invisible" /> 
     </RelativeLayout> 

    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

<android.support.design.widget.TabLayout 
    android:id="@+id/tabLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    app:tabGravity="fill" 
    app:tabIndicatorHeight="0dp" 
    app:tabMode="fixed" /> 

<FrameLayout 
    android:id="@+id/frameContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/tabLayout" 
    android:layout_below="@id/appbar" /> 

です。

public class MyFragment extends BaseFragment { 


public MyFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_cldirtanlar, container, false); 

    setToolbar(view, "New Title", View.VISIBLE); 

    return view; 
} 

public void setToolbar(View view, String title, int addButton) { 
     TextView txtToolbarTitle = (TextView) view.findViewById(R.id.txtToolbarTitle); 
     txtToolbarTitle.setText("Kontlar"); 
     ImageButton imbToolbarAdd = (ImageButton) view.findViewById(R.id.imbToolbarAdd); 
     imbToolbarAdd.setVisibility(addButton); 
} 

}あなたは、あなたのMainActivityに設定されたあなたのR.layout.fragment_cldirtanlarではなく、ビュー内のビューを返しますview.findViewById()を、使用している

+0

どこがnullを返しますか?そして、あなたのフラグメントのライフサイクルで何が起こっているかを確認しましたか?それがアクティビティのライフサイクルとどのように相関しているか。 – JoxTraex

+0

txtToolbarTitleはnullを返します。そして、私はクラス内の要素にアクセスしません、アプリケーションは正常に動作します – ssss

答えて

2

アクティビティによって作成されたビューを取得する場合は、代わりにgetActivity().findViewById()を使用できます。

+0

ありがとうございました。魅力のように動作します。 – ssss

+1

あなたの質問に答えた場合は、回答を受け入れるようにしてください。運が良かった! – ianhanniballake

+0

実際、昨日私は正しい答えとしてそれを受け入れましたが、そのような短時間で承認しないように警告してくれたので、それは残っていました。 :) 再度、感謝します。 – ssss

関連する問題