2017-02-13 1 views
0

활동의 데이터를 조각으로 전달하려고합니다. 나는 해결책을 둘러 보았지만 이것이 나를 위해 일할 수 없었습니다. 내가 번들을 사용했지만 null 포인터 예외가 발생했습니다.활동에서 조각으로 데이터를 전달하려고 할 때 null 포인터입니다. 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); 

그리고 내 조각 클래스의

내가 이것을 가지고 : 나는 안드로이드 tablayout 템플릿을 사용하여이 activty 두 조각을하고

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

.

내가 점점 오전 오류는 다음과 같습니다 :

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

FragmenTransaction은 어떻게 생겼습니까? –

+0

이 솔루션을 사용해보십시오. http://stackoverflow.com/questions/32031178/nullpointerex-x-android-os-bundle –

+0

여기에 전화 코드를 게시 할 수 있습니까 –

답변

0

, 당신은

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

외에 활동에받는 하나를 사용하여 조각으로 이동하는 대신하는 새로운 번들 인스턴스를 만들려고하세요 이걸로 내가 틀린 것을 볼 수 있을지 모르겠다. 희망한다.

0

번들은 안에있다. null이됩니다.

당신은

onCreateView에서 번들의 데이터 (위해 LayoutInflater 인플레이터, 뷰 그룹 용기, 번들 savedInstance)를 얻고 구성원 개체에 데이터를 할당하고 onMapReady에 사용해야합니다.

관련 문제