为什么 findViewById() returns "null"?

Why does findViewById() returns "null"?

我有一个关于 java
的重要问题 我有这个代码:

package fr.christian.lbcde;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import fr.christian.lbcde.ui.main.PageViewModel;
import fr.christian.lbcde.ui.main.SectionsPagerAdapter;

public class MainActivity extends AppCompatActivity {

    PageViewModel pageViewModel;
    View activity_main, tab_create, tab_connect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        FloatingActionButton fab = findViewById(R.id.fab);

        tab_connect = findViewById(R.id.tab_connect);
        tab_create = findViewById(R.id.tab_create);
        System.out.print("Result of tab_connect : ");
        System.out.print(findViewById(R.id.tab_connect));
        System.out.print(" ");
        System.out.println(tab_connect==null);
        System.out.print("Result of tab_create : ");
        System.out.print(findViewById(R.id.tab_create));
        System.out.print(" ");
        System.out.println(tab_create==null);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addNew();
            }
        });
//        hideAll();
//        tab_connect.setVisibility(View.VISIBLE);
        pageViewModel.addTabListener(new PageViewModel.tabChangeListener() {
            @Override
            public void onChangeTab(View view, int tab) {
                switch (tab) {
                    case 1:
                        hideAll();
                        tab_connect.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        hideAll();
                        tab_create.setVisibility(View.VISIBLE);
                        break;
                    default:
                        new Toast(getApplicationContext())
                                .makeText(getApplicationContext(), "Rien a afficher pour cet onglet", Toast.LENGTH_SHORT)
                                .show();
                }
            }
        });
    }

    public void addNew() {
        new Toast(getApplicationContext())
                .makeText(getApplicationContext(), "Cette option n'est pas encore disponible", Toast.LENGTH_SHORT)
                .show();
    }

    public void hideAll() {
        View[] views = {
                tab_create,
                tab_connect
        };
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.INVISIBLE);
        }
        ;
    }

}

和我的布局的四个文件:

activity main.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.LeBonCoinDeLÉvangile.AppBarOverlay">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabSelectedTextColor="?attr/colorPrimaryVariant"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:background="?attr/colorPrimary"
        app:srcCompat="@android:drawable/ic_menu_info_details" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>


片段_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.PlaceholderFragment">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">
            <include layout="@layout/login_main"/>
            <include layout="@layout/create_main"/>
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

创建_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab_create"
    android:orientation="vertical"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/activity_vertical_margin"
    android:visibility="invisible">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TAB_CREATE"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BONJOUR LES ÉLËPHANTS"/>
</LinearLayout>

和登录_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab_connect"
    android:orientation="vertical"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/activity_vertical_margin">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TAB_CONNECT"/>
    <TextView
        style="@style/h2textStyle"
        android:text="@string/titleConnect_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/loginConnect_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />
</LinearLayout>



问题是在以下几行:
System.out.print(findViewById(R.id.tab_connect));


System.out.print(findViewById(R.id.tab_create));

我得到了这个结果(对于两者):

null

所以我无法删除 hideAll()tab_create.setVisibility(View.VISIBLE) 等中的错误“java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.view.View.setVisibility(int)'” ...因为当我初始化我的布局时,它们是用 一个空对象 (而不是我的布局)初始化的。
我添加了一些行,例如

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TAB_CREATE"/>

在我的文件中查看这些选项卡是否真的是我在屏幕上看到的选项卡,并且看起来我确实看到了它们(当我取消具有布局可见性的行时)。
感谢您的帮助!

您的 tab_connect 等在 fragment_main 布局中 included 而不是在您为这些 ID 调用 findViewById() 时膨胀的 activity_main 布局.您发布的代码没有显示您使用 fragment_main 的位置,但可以肯定的是,当您尝试查找这些视图时,它不在您的 activity 的视图层次结构中。

要么将 findViewById() 调用以及您对视图所做的任何操作移动到片段,要么将视图从片段移动到主 activity。