不同布局中的 findViewById(找不到 BottomNavigationView)
findViewById in different layout (cannot find BottomNavigationView)
我正在开发一个应用程序,因此当您单击登录按钮时,它会显示一个带有底部菜单的屏幕。但是我在将侦听器设置为菜单中的项目时遇到了问题。问题是当我尝试使用 findViewById 查找我的 BottomNavigationView 时,它会在我的 activity_main 而不是我放置它的片段 (welcome_fragment) 中寻找它,因为我只希望它在什么时候显示有人已登录。因此我将获得 null 作为 BottomNavigationView。
我已经尝试在我的方法 "welcomePage" 中设置 setContentView(R.layout.welcome_fragment) ,它确实做到了,这样它就会找到它并且它不会为空。但是,这会导致许多其他错误,例如
“没有找到 id 0x7f080032 (com.example.MyAPP:id/container) 片段 WelcomePage{9547e9f #1 id=0x7f080032} 的视图”,所以我认为这不是正确的处理方式。
这是我的 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 因为我只想在用户登录时显示它。我该如何解决这个问题?
试试这个,应该可以。
欢迎页面
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);
}
}
主要活动
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();
}
}
我正在开发一个应用程序,因此当您单击登录按钮时,它会显示一个带有底部菜单的屏幕。但是我在将侦听器设置为菜单中的项目时遇到了问题。问题是当我尝试使用 findViewById 查找我的 BottomNavigationView 时,它会在我的 activity_main 而不是我放置它的片段 (welcome_fragment) 中寻找它,因为我只希望它在什么时候显示有人已登录。因此我将获得 null 作为 BottomNavigationView。
我已经尝试在我的方法 "welcomePage" 中设置 setContentView(R.layout.welcome_fragment) ,它确实做到了,这样它就会找到它并且它不会为空。但是,这会导致许多其他错误,例如 “没有找到 id 0x7f080032 (com.example.MyAPP:id/container) 片段 WelcomePage{9547e9f #1 id=0x7f080032} 的视图”,所以我认为这不是正确的处理方式。
这是我的 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 因为我只想在用户登录时显示它。我该如何解决这个问题?
试试这个,应该可以。 欢迎页面
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);
}
}
主要活动
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();
}
}