【问题标题】:null pointer when trying to pass data from activity to fragment Android尝试将数据从活动传递到片段Android时的空指针
【发布时间】:2017-02-13 14:02:19
【问题描述】:

我正在尝试将数据从活动传递到片段。我已经查看了解决方案,但无法让它为我工作。我尝试使用捆绑包,但出现空指针异常。

活动类:

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);

在我的片段类中,我有这个:

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);

我使用的是 android tablayout 模板,并且在这个活动中有两个片段。

我得到的错误如下:

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

【问题讨论】:

  • 您的 FragmenTransaction 看起来如何?
  • 你可以在这里发布调用活动代码
  • 请通过clickin将数据从活动传递到Android片段?解释一下

标签: java android android-activity fragment


【解决方案1】:

请尝试创建一个新的 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);

除此之外,我不确定我发现了什么问题, 希望对你有帮助

【讨论】:

    【解决方案2】:

    Bundle 在 onMapReady 方法中。它将为空。

    您应该从 onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) 中的 bundle 获取数据

    并将数据分配给成员对象,然后在 onMapReady 中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多