为什么当我想在工具栏的布局中访问TextView时将其变为null

ssss

我想访问工具栏布局中的一个元素,但是它变为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" />

这是我要访问TextView的地方;

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);
}

}

伊恩汉尼巴拉克

您正在使用view.findViewById(),它只会返回您的R.layout.fragment_cldirtanlar视图,而不会返回您视图中设置的视图MainActivity

getActivity().findViewById()如果要检索您的活动创建的视图,则可以使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我点击文本框时,为什么工具栏没有显示?

从android中的Fragment访问工具栏textview

为什么 setSupportActionBar() 不能识别我的工具栏?

为什么从约束布局1.0.2切换到1.1.3会破坏我的自定义工具栏?

为什么正文中的按钮与我的 NavigationView 工具栏中的按钮的行为不同?

为什么我工具栏中的帮助按钮不起作用

为什么我的 Tinymce 工具栏菜单中没有显示样式选择按钮?

材料工具栏中的弹性布局

删除工具栏布局中的空白

工具栏中的布局问题

我想在隐藏的工具栏上滑动,但是错了

为什么工具栏被TabLayout覆盖?

当我向上滚动RecyclerView时,工具栏和TabLayout不会隐藏

当我向上滚动RecyclerView时,工具栏和TabLayout不会隐藏

当我设置为工具栏的 src 菜单项时,Imageview 不可见

当我尝试从ViewModel以编程方式设置工具栏徽章计数时,无法使用

当我按下工具栏时,Viewpager 被向上推

当我尝试导入 SVG 图像时,在工具栏中出现此错误“无法编译”

如何访问工具栏中的按钮?

Android为什么不能在工具栏中使用TextView

滚动RecyclerView时隐藏工具栏时的布局抖动

为什么当我在地址栏中输入新搜索内容时,Chrome浏览器会更改tabId,然后将其导航到Google页面结果

当我将FragmentPagerAdapter和ViewPager一起使用时,为什么在活动中看到两个工具栏?

当我想在android中动态创建按钮时,为什么我的应用程序不起作用?

当我在搜索栏android中快速删除文本时,为什么我的recyclerview无法重置?

当我按工具栏中的向后箭头获取空对象引用时

为什么我的Chrome浏览器检查器工具栏移到顶部?以及如何将其放回原处?

我在Android Studio的布局编辑器中找不到工具栏

底部工具栏布局