膨胀后找不到自定义视图的子元素



我试图在活动负载上膨胀一个自定义视图,然后用该视图的子元素做一些事情。下面我尝试膨胀视图,然后在布局XML中获取对TextView的引用以更新文本值。然而在下面的代码中,在最后一行之后,actionBarTitle是空的-当我期望它是一个对TextView的引用。我做错了什么?

活动onCreate()方法

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_userseed);
LayoutInflater inflator = LayoutInflater.from(this);
        actionBarView = inflator.inflate(R.layout.fragment_actionbar_viewseed, null);
        TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.action_bar_title);
}       

和布局XML

<?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"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp" />
    <android.support.v4.view.ViewPager     
        android:id="@+id/actionbar_pager"     
        android:layout_width="match_parent"     
        android:layout_height="match_parent">
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/actionbar_pagerstrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
         />
    </android.support.v4.view.ViewPager>
</LinearLayout>

尝试:

TextView actionBarTitle = (TextView) actionBarView.findViewById(R.id.actionbar_title);

最新更新