所以我试图从活动类的片段类中调用一个方法。在我的活动中,我有底部导航视图(和其中的PopupMenu),试图从片段中调用方法。
这是我使用谷歌搜索解决方案进行的活动,但不适用于我:
public class MenuActivity extends AppCompatActivity {
MapFragment map = new MapFragment();
TextView textView;
Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
toolbar = (Toolbar) findViewById(R.id.toolbar);
((TextView) toolbar.findViewById(R.id.text_main)).setText(MainActivity.selectedButton);
Fragment selectedFragment = null;
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_map);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.nav_map:
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
break;
case R.id.nav_sensors:
selectedFragment = new SensorsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
break;
case R.id.nav_apps:
PopupMenu popup = new PopupMenu(MenuActivity.this, findViewById(R.id.nav_apps));
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.bottom_nav_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.nav_floor:
map.test();
//Toast.makeText(getApplicationContext(),"Floor",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_room:
Toast.makeText(getApplicationContext(),"Room",Toast.LENGTH_SHORT).show();
break;
case R.id.nav_search:
Toast.makeText(getApplicationContext(),"Search",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
};
}
还有我的片段(我在该类中还有其他几种Maps方法,但这是我正在测试的方法):
public class MapFragment extends Fragment{
View mView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_map, container, false);
return mView;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
public void test(){
Toast.makeText(getActivity(),"Test",Toast.LENGTH_SHORT).show();
}
}
此外,XML文件:
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:id="@+id/toolbar">
<TextView
android:id="@+id/text_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_centerHorizontal="true"/>
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_nav_map"
android:layout_below="@+id/toolbar"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_nav_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_nav"
app:itemIconTint="@drawable/bottom_nav_color"
app:itemTextColor="@drawable/bottom_nav_color"
android:background="@color/colorPrimary"
app:labelVisibilityMode="labeled"
/>
</RelativeLayout>
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/location_button"
android:text="LOC"
android:layout_gravity="bottom"
/>
</FrameLayout>
总而言之,当我在弹出菜单中选择第一个选项时,我试图调用方法test()。
答案 0 :(得分:0)
您正在对从未添加到片段管理器的片段实例调用test
。
// You initialize this field
MapFragment map = new MapFragment();
protected void onCreate(Bundle savedInstanceState) {
...
// Then you initialize a new MapFragment you add to the fragment manager
Fragment selectedFragment = null;
selectedFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
}
...
// Then you call `test` on the instance that's not attached to anything
map.test();
解决方案是使用一个片段并进行引用。而且,您还需要注意正确处理和恢复片段状态。
MapFragment map; // Declare but don't initialize here
protected void onCreate(Bundle savedInstanceState) {
...
// Initialize fragment instead of creating a new one if there is no state already
if (savedInstanceState == null) {
map = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, map).commit();
}
else {
// If there is state, the fragment will be restored by the system, so just
// get a reference to it
map = getSupportFragmentManager().findFragmentById(R.id.fragment_containter)
}
}
...
// Now calling test on the correctly attached fragment should work
map.test();
希望有帮助!