使用不同布局的findViewById(找不到BottomNavigationView)

时间:2019-03-31 11:24:03

标签: android android-fragments findviewbyid

我正在做一个应用程序,因此当您单击登录按钮时,它将显示一个带有底部菜单的屏幕。但是我在将侦听器设置为菜单中的项目时遇到了问题。问题是当我尝试使用findViewById查找我的BottomNavigationView时,它将在我的activity_main而不是我放置它的片段(welcome_fragment)中寻找它,因为我只希望它在有人登录时显示。因此作为BottomNavigationView将获得null。

我尝试在我的方法“ welcomePage”中设置setContentView(R.layout.welcome_fragment),这样做确实可以使它找到并不会为null。但是,这会导致很多其他错误,例如 “找不到片段WelcomePage {9547e9f#1 id = 0x7f080032}的ID 0x7f080032(com.example.MyAPP:id / container)的视图,”因此,我认为这不是正确的解决方法。

这是我的MainActivity:

    public class MainActivity extends AppCompatActivity {

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


        FragmentManager fm = getFragmentManager();
        Login loggingIn= new Login();
        fm.beginTransaction().replace(R.id.container, loggingIn).commit();
    }

     //When button is pressed we get into this function
     public void finishProcess(){

        WelcomePage wp = new WelcomePage();
        FragmentManager fm = getFragmentManager();
        fm.beginTransaction().addToBackStack(null);
        fm.beginTransaction().replace(R.id.container, wp).commit();


        try {
         //Here is where I get null, because we are looking in activity_main
         BottomNavigationView bottomNav=findViewById(R.id.bottomnavigate);
         bottomNavigation.setOnNavigationItemSelectedListener(menuListener);}
            catch (Exception e) {
                System.out.println(e);
            }
        }}

    //Have tried this one and it works fine
    private BottomNavigationView.OnNavigationItemSelectedListener menuListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() {...}
}

这是我的WelcomeFragment:

    public class WelcomePage extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
        return inflater.inflate(R.layout.welcome_fragment, container, false)}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

这是我的welcome_fragment布局:


<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WelcomePage"
    android:background="@drawable/welcomegradient">

    <FrameLayout
        android:id="@+id/showing_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottomnavigate">
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomnavigate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/mymenu"
        android:background="?android:attr/windowBackground" />


</RelativeLayout>

正如我之前提到的,问题在于findViewById找不到BottomNavigationView,因为它正在查看activity_main而不是welcome_fragment,并且我不想将菜单放在activity_main中,因为我只想在用户使用时显示它已登录。如何解决此问题?

1 个答案:

答案 0 :(得分:0)

尝试一下,它应该可以工作。 欢迎页面

public class WelcomePage extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.welcome_fragment, container, false);

        //Have tried this one and it works fine
        BottomNavigationView.OnNavigationItemSelectedListener menuListener 
        = new BottomNavigationView.OnNavigationItemSelectedListener() {...}

        BottomNavigationView bottomNav = view.findViewById(R.id.bottomnavigate);
        bottomNav.setOnNavigationItemSelectedListener(menuListener);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

MainActivity

   public class MainActivity extends AppCompatActivity {

   private FragmentManager fm;

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

       fm = getSupportFragmentManager();
       Login loggingIn= new Login();
       fm.beginTransaction().replace(R.id.container, loggingIn).commit();
   }

   //When button is pressed we get into this function
   public void finishProcess() {

        WelcomePage wp = new WelcomePage();
        fm.beginTransaction().addToBackStack(null);
        fm.beginTransaction().replace(R.id.container, wp).commit();
    }
}