【发布时间】: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