ツールバーのレイアウト内の要素にアクセスしたいが、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()
を、使用している
どこがnullを返しますか?そして、あなたのフラグメントのライフサイクルで何が起こっているかを確認しましたか?それがアクティビティのライフサイクルとどのように相関しているか。 – JoxTraex
txtToolbarTitleはnullを返します。そして、私はクラス内の要素にアクセスしません、アプリケーションは正常に動作します – ssss