简体   繁体   English

尝试将数据从活动传递到片段Android时返回null指针

[英]null pointer when trying to pass data from activity to fragment Android

I am trying to pass data from activity to a fragment. 我正在尝试将数据从活动传递到片段。 I have looked around at solutions but unable to get this to work for me. 我到处都在寻找解决方案,但是无法为我工作。 I've tried using the bundle but I am getting null pointer exception. 我尝试使用捆绑软件,但出现空指针异常。

Activity Class: 活动类别:

public class RouteOutput extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

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

    Bundle ways = getIntent().getExtras();
    if(ways==null) {
        return;
    }
    System.out.println("START LAT: " + ways.getDouble("start_lat"));



    ways.putDouble("start_lat", ways.getDouble("start_lat"));
    ways.putDouble("start_lon", ways.getDouble("start_lon"));
    ways.putStringArrayList("coords", ways.getStringArrayList("coords"));

    if(!(ways.getDouble("altend") == 0)) {
        ways.putDouble("altend_lat", ways.getDouble("altend_lat"));
        ways.putDouble("altend_lon", ways.getDouble("altend_lon"));
    }
    FragmentMapOutput frag = new FragmentMapOutput();
    frag.setArguments(ways);

And in my fragment class i have this: 在我的片段类中,我有这个:

View view = inflater.inflate(R.layout.fragment_map_output, container, false);
    MapboxAccountManager.start(getContext(), getString(R.string.access_token));

    mView = (MapView) view.findViewById(R.id.mapview);
    mView.onCreate(savedInstanceState);
    mView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            mMap = mapboxMap;

            Bundle bundle = getArguments();
            double startlat = bundle.getDouble("start_lat");
            System.out.println("BUNDLE: " + startlat);

I am using the android tablayout template and have two fragments in this activty. 我正在使用android tablayout模板,并且在此活动中有两个片段。

The error i am getting is below: 我得到的错误如下:

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.os.BaseBundle.getDouble(java.lang.String)' on a null object reference

Please, try to create a new Bundle instance that will go to fragment rather than using the one you receive in activity 请尝试创建一个新的Bundle实例,它将实例化,而不是使用您在活动中收到的实例

Bundle newWays=new Bundle();
  Bundle ways = getIntent().getExtras();
if(ways==null) {
    return;
}


 newWays.putDouble("start_lat", ways.getDouble("start_lat"));
newWays.putDouble("start_lon", ways.getDouble("start_lon"));
newWays.putStringArrayList("coords", ways.getStringArrayList("coords"));

if(!(ways.getDouble("altend") == 0)) {
    newWays.putDouble("altend_lat", ways.getDouble("altend_lat"));
    newWays.putDouble("altend_lon", ways.getDouble("altend_lon"));
}
FragmentMapOutput frag = new FragmentMapOutput();
frag.setArguments(newWays);

Apart from this, I am not sure I see anything wrong, Hope it helps 除此之外,我不确定是否看到任何错误,希望对您有所帮助

Bundle is inside onMapReady method. 捆绑包位于onMapReady方法中。 It will be null. 它将为空。

You should get the data from bundle in onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) 您应该从onCreateView中的捆绑包中获取数据(LayoutInflater充气器,ViewGroup容器,捆绑包saveInstance)

and assign the data to member objects and then use in onMapReady. 并将数据分配给成员对象,然后在onMapReady中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将数据从活动传递到片段时,总是得到 null 值 - Got null value always when trying to pass data from activity to fragment 为什么我的数据在尝试将其从我的活动传递到片段时显示为 null? - Why is my data being displayed as null when trying to pass it from my activity to fragment? 尝试将包从我的活动传递到我的片段时,为什么我不断收到 null? - Why do I keep getting null when trying to pass a bundle from my activity to my fragment? 从活动中调用片段会引发空指针异常-Android - calling fragment from activity throws null pointer exception- Android 将数据从活动传递到片段时发生的问题 - Issue when pass data from activity to fragment 当我将数据从活动传递到活动时,获取 Null 指针 Exp - Getting Null Pointer Exp when i pass Data from activity to activity 空指针异常将捆绑数据从活动发送到片段 - Null Pointer Exception Sending Bundle Data From Activity to Fragment 从片段向活动传递值时,空指针异常 - Null Pointer Exception when passing value from Fragment to Activity 在活动中按下时如何将数据从活动传递到片段? - android - How to pass data from activity to Fragment when pressed back in activity ? - android 初始加载Fragment时,将数据从Activity传递到Fragment - Pass Data to Fragment from Activity, when intially loaded with Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM