Java 通过具有底部导航视图的活动在片段之间传递值

Java 通过具有底部导航视图的活动在片段之间传递值,java,android,android-fragments,android-activity,bottomnavigationview,Java,Android,Android Fragments,Android Activity,Bottomnavigationview,我试图通过MainActivity将自动更新的文本(即纬度和经度信息)从fragment_map传递给fragment_clocking,但我在fragment_clocking中获得的值返回为null,在Logcat中没有错误 我试图使用bundle传递值,但我不确定在哪里以及如何声明和调用bundle,因为我有一个底部导航视图 MainActivity.java public class MainActivity extends AppCompatActivity { Te

我试图通过MainActivity将自动更新的文本(即纬度和经度信息)从fragment_map传递给fragment_clocking,但我在fragment_clocking中获得的值返回为null,在Logcat中没有错误

我试图使用bundle传递值,但我不确定在哪里以及如何声明和调用bundle,因为我有一个底部导航视图

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    TextView latitude;
    TextView longitude;
    Bundle bundle = new Bundle();

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

        BottomNavigationView bottomNav = findViewById(R.id.nav_view);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        latitude = findViewById(R.id.id_latitude);
        longitude = findViewById(R.id.id_longitude);

        bundle.putString("latitudeInfo", String.valueOf(latitude));
        bundle.putString("longitudeInfo", String.valueOf(longitude));

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_map:
                        selectedFragment = new MapFragment();
                        selectedFragment.setArguments(bundle);
                        break;
                    case R.id.navigation_clocking:
                        selectedFragment = new ClockingFragment();
                        selectedFragment.setArguments(bundle);
                        break;

                }

                getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, selectedFragment)
                .commit();
                return true;
                }
        };
    }
    public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {

    TextView latitude;
    TextView longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, null);

        latitude = rootView.findViewById(R.id.id_latitude);
        longitude = rootView.findViewById(R.id.id_longitude);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

        latitude.setText("Longitude: "+ location.getLongitude());
        longitude.setText("Latitude:    "+ location.getLatitude());
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

            mGoogleApiClient.connect();
        }
    }
    public class ClockingFragment extends Fragment implements LocationListener {

    TextView latitude, longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_clocking, null);

        latitude = rootView.findViewById(R.id.id_latitudeLocation);
        longitude = rootView.findViewById(R.id.id_longitudeLocation);

       Bundle bundle = getArguments();
       if(bundle != null){
           String latitudetest = bundle.getString("latitudeInfo");
           String longitudetest = bundle.getString("longitudeInfo");

           latitude.setText(latitudetest);
           longitude.setText(longitudetest);
       }

        return rootView;
    }
MapFragment.java

    public class MainActivity extends AppCompatActivity {

    TextView latitude;
    TextView longitude;
    Bundle bundle = new Bundle();

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

        BottomNavigationView bottomNav = findViewById(R.id.nav_view);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        latitude = findViewById(R.id.id_latitude);
        longitude = findViewById(R.id.id_longitude);

        bundle.putString("latitudeInfo", String.valueOf(latitude));
        bundle.putString("longitudeInfo", String.valueOf(longitude));

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_map:
                        selectedFragment = new MapFragment();
                        selectedFragment.setArguments(bundle);
                        break;
                    case R.id.navigation_clocking:
                        selectedFragment = new ClockingFragment();
                        selectedFragment.setArguments(bundle);
                        break;

                }

                getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, selectedFragment)
                .commit();
                return true;
                }
        };
    }
    public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {

    TextView latitude;
    TextView longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, null);

        latitude = rootView.findViewById(R.id.id_latitude);
        longitude = rootView.findViewById(R.id.id_longitude);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

        latitude.setText("Longitude: "+ location.getLongitude());
        longitude.setText("Latitude:    "+ location.getLatitude());
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

            mGoogleApiClient.connect();
        }
    }
    public class ClockingFragment extends Fragment implements LocationListener {

    TextView latitude, longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_clocking, null);

        latitude = rootView.findViewById(R.id.id_latitudeLocation);
        longitude = rootView.findViewById(R.id.id_longitudeLocation);

       Bundle bundle = getArguments();
       if(bundle != null){
           String latitudetest = bundle.getString("latitudeInfo");
           String longitudetest = bundle.getString("longitudeInfo");

           latitude.setText(latitudetest);
           longitude.setText(longitudetest);
       }

        return rootView;
    }
ClockingFragment.java

    public class MainActivity extends AppCompatActivity {

    TextView latitude;
    TextView longitude;
    Bundle bundle = new Bundle();

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

        BottomNavigationView bottomNav = findViewById(R.id.nav_view);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        latitude = findViewById(R.id.id_latitude);
        longitude = findViewById(R.id.id_longitude);

        bundle.putString("latitudeInfo", String.valueOf(latitude));
        bundle.putString("longitudeInfo", String.valueOf(longitude));

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_map:
                        selectedFragment = new MapFragment();
                        selectedFragment.setArguments(bundle);
                        break;
                    case R.id.navigation_clocking:
                        selectedFragment = new ClockingFragment();
                        selectedFragment.setArguments(bundle);
                        break;

                }

                getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, selectedFragment)
                .commit();
                return true;
                }
        };
    }
    public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {

    TextView latitude;
    TextView longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_map, null);

        latitude = rootView.findViewById(R.id.id_latitude);
        longitude = rootView.findViewById(R.id.id_longitude);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

        latitude.setText("Longitude: "+ location.getLongitude());
        longitude.setText("Latitude:    "+ location.getLatitude());
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

            mGoogleApiClient.connect();
        }
    }
    public class ClockingFragment extends Fragment implements LocationListener {

    TextView latitude, longitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_clocking, null);

        latitude = rootView.findViewById(R.id.id_latitudeLocation);
        longitude = rootView.findViewById(R.id.id_longitudeLocation);

       Bundle bundle = getArguments();
       if(bundle != null){
           String latitudetest = bundle.getString("latitudeInfo");
           String longitudetest = bundle.getString("longitudeInfo");

           latitude.setText(latitudetest);
           longitude.setText(longitudetest);
       }

        return rootView;
    }
活动\u main.xml

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />
    <FrameLayout
        android:id="@+id/fragment_clocking"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitudeLocation"
            android:layout_below="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_map" />

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity" />

        <TextView
            android:id="@+id/id_latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_below="@+id/id_latitude"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

fragment\u clocking.xml

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />
    <FrameLayout
        android:id="@+id/fragment_clocking"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitudeLocation"
            android:layout_below="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_map" />

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity" />

        <TextView
            android:id="@+id/id_latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_below="@+id/id_latitude"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

片段映射.xml

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />
    <FrameLayout
        android:id="@+id/fragment_clocking"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitudeLocation"
            android:layout_below="@+id/id_latitudeLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:text="TEST PLACEHOLDER"
            android:textSize="30sp"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_map" />

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity" />

        <TextView
            android:id="@+id/id_latitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/id_longitude"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_below="@+id/id_latitude"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textSize="30sp" />

预期结果:FragmentMap.java中的纬度和经度值通过MainActivity.java并显示在FragmentClocking.java中


实际结果:

一般来说,如果您有一个收集数据的
片段,并且希望将此数据传递给另一个
片段,那么您应该在
活动中实现一个回调接口。收集数据的
片段应使用回调接口调用
活动中的方法。
活动应实现回调接口。调用
活动中的方法时,该方法应将数据存储在某些成员变量中。当
活动
启动另一个
片段
时,它应该将以前存储的数据添加到“arguments”
捆绑包
,然后调用
setArguments()
将数据传递到新的
片段


有关实现此功能的几种可能方法,请参阅。

在创建
活动时,将纬度和经度的值放入
包中。我不清楚这些数据应该来自哪里。您希望从哪里获得经度和纬度?请解释一下