[英]Include in Android: findViewById returns null
I have some app. 我有一些应用。 I wrote and tested it on Android 5.1. 我在Android 5.1上进行了编写和测试。 When I tried to run my app on Android 6.0 I've got the exception. 当我尝试在Android 6.0上运行我的应用程序时,我遇到了例外。 The exception appeare after I try to bind a FloatingActionButton and set OnClickListener because of null. 我尝试绑定FloatingActionButton并将OnClickListener设置为null后,会出现异常。 FindViewById returns null when I try to get it with FrameLayout from "include" directive. 当我尝试使用FrameLayout从“ include”指令获取它时,FindViewById返回null。
MainActivity.java 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 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 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>
When I use SDK >= 23 I have the exception and if SDK < 23 it's OK. 当我使用SDK> = 23时,我有例外,如果SDK <23,就可以了。 How can I solve it? 我该如何解决?
UPDATE: 更新:
In activity_main.xml
I changed from: 在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" />
to: 至:
<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" />
After it fab_settings
is still null. 之后, fab_settings
仍然为空。 I don't understand why! 我不明白为什么!
I solved my problem. 我解决了我的问题。
It was my mistake. 是我的错 I didn't notice the new folder layout-v23
where the activity was exist. 我没有注意到活动所在的新文件夹layout-v23
。 My problem was solved after deleting layout-v23
. 删除layout-v23
之后,我的问题解决了。
Thank all of you. 谢谢大家
First find framelayout with fab_frame id. 首先找到带有fab_frame id的framelayout。 Second framelayout.findviewbyid(your fab id) Check this question findViewById not working for an include? 第二个framelayout.findviewbyid(您的晶圆厂ID)检查此问题findViewById无法用于包含吗?
You haven't declared "fabOnClick" & "setsOnClick". 您尚未声明“ fabOnClick”和“ setsOnClick”。 Declare them, hopefully it will solve the problem. 声明它们,希望它将解决问题。
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(fabOnClick);
fab_settings = (FloatingActionButton) findViewById(R.id.fab_settings); //
fab_settings.setOnClickListener(setsOnClick); // <---- Exception
View frame = findViewById(R.id.fab_frame);
fab_settings = frame.findViewById(R.id.fab_settings);
fab_settings.setOnClickListener(setsOnClick);
You can write something like this for FloatingActionButton for the setOnClickListerner method. 您可以为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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.