包含在Android中:findViewById返回null

时间:2018-03-16 20:24:31

标签: java android findviewbyid

我有一些应用程序。 我在Android 5.1上编写并测试了它。当我尝试在Android 6.0上运行我的应用程序时,我得到了例外。在我尝试绑定FloatingActionButton并因为null而设置OnClickListener之后出现异常。当我尝试从“include”指令使用FrameLayout获取它时,FindViewById返回null。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Context instance;

    private MainPagerAdapter mSectionsPagerAdapter;

    public ViewPager mViewPager;

    FloatingActionButton fab;
    FloatingActionButton fab_settings;
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), this);

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(fabOnClick);

        fab_settings = (FloatingActionButton) findViewById(R.id.fab_settings); // <---- Returns null

        fab_settings.setOnClickListener(setsOnClick); // <---- Exception
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.vjs.classesschedule.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true"
        android:background="@color/color_title">

        <TextView
            android:text="@string/title_activity_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textSize="24sp"
            android:textColor="@android:color/black"
            android:background="@drawable/bg_title4" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:visibility="gone">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/tab" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:ignore="UnusedAttribute"
        android:scrollbarAlwaysDrawVerticalTrack="false"
        android:layout_alignParentTop="true"
        android:scrollbarAlwaysDrawHorizontalTrack="false" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_menu_white_24dp"
        app:fabSize="mini" />

    <include layout="@layout/fab_layout" android:id="@+id/fab_frame"/>

</android.support.design.widget.CoordinatorLayout>

fab_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent" android:id="@+id/fab_frame">

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_settings"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_settings_white_24dp"
            android:visibility="invisible"
            app:fabSize="mini"
            app:backgroundTint="@android:color/holo_purple"/>

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_dic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_assignment_white_24dp"
            android:visibility="invisible"
            app:fabSize="mini"
            app:backgroundTint="@android:color/holo_orange_dark"/>

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_add_white_24dp"
            android:visibility="invisible"
            app:fabSize="mini"
            app:backgroundTint="@android:color/holo_green_dark"/>
</FrameLayout>

当我使用SDK&gt; = 23时,我有例外,如果SDK&lt; 23没关系。 我该如何解决?

更新

activity_main.xml我改变了:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@drawable/ic_menu_white_24dp"
    app:fabSize="mini" />

为:

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_menu_white_24dp"
        app:fabSize="mini" />
 <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_menu_white_24dp"
        app:fabSize="mini" />

之后fab_settings仍然为空。我不明白为什么!

5 个答案:

答案 0 :(得分:1)

我解决了我的问题。

这是我的错。我没有注意到活动存在的新文件夹backslash。删除layout-v23后,我的问题就解决了。

谢谢大家。

答案 1 :(得分:0)

首先找到具有fab_frame id的framelayout。第二个framelayout.findviewbyid(您的fab id) 请查看此问题findViewById not working for an include?

答案 2 :(得分:0)

你还没有宣布&#34; fabOnClick&#34; &安培; &#34; setsOnClick&#34 ;.声明它们,希望它能解决问题。

fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(fabOnClick);

            fab_settings = (FloatingActionButton) findViewById(R.id.fab_settings); // 

            fab_settings.setOnClickListener(setsOnClick); // <---- Exception

答案 3 :(得分:0)

View frame = findViewById(R.id.fab_frame);

fab_settings = frame.findViewById(R.id.fab_settings);
fab_settings.setOnClickListener(setsOnClick); 

答案 4 :(得分:-1)

对于setOnClickListerner方法,您可以为FloatingActionButton编写类似的内容。

 fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    //Write here anything that you wish to do on click of FAB
}
});