我試圖將數據從活動傳遞到片段。我環顧瞭解解決方案,但無法爲我工作。我試過使用這個包,但我得到空指針異常。嘗試將數據從活動傳遞到片段時出現空指針Android
活動課:
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模板,並在此activty兩個片段。
我得到的錯誤是下面:
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是怎麼樣的? –
試一試這個解決方案http://stackoverflow.com/questions/32031178/nullpointerexception-on-android-os-bundle –
你能在這裏發佈調用活動代碼嗎 –