這是我的代碼的一個片段。
'''
Bundle b= new Bundle();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState)。
adapter= new PlatformAdapter(資料)。
recyclerView=view.findViewById(R.id.recyclerView)。
recyclerView.setLayoutManager( new LinearLayoutManager(getContext())。
recyclerView.setAdapter(adapter)。
RequestVolley.getInstance(getContext())
.doGetRequest("http://www...... .json", new RequestVolley.OnCompleteCallback() {
@Override
public void onCompleted(String response) {
try {
JSONArray jsonArray = new JSONArray( response)。
for(int i=0; i<jsonArray.length(); i ){
JSONObject object= jsonArray.optJSONObject(i);
if(object!=null){
data.add(Platform.parseJSON(object))。
}
}
adapter.notifyDataSetChanged()。
}catch(JSONException e){
e.printStackTrace()。
}
}
});
b.putSerializable("ListPlatforms", (Serializable) data)。
}
然后,我試圖在另一個片段中這樣做。 '''
@Override
public void onLocationChanged(@NonNull Location location) {
if (googleMap != null) {
if (myMarker == null) {
MarkerOptions options = new MarkerOptions() 。
options.title("My location")。
options.position(new LatLng(location.getLatitude(), location.getLongitude())。
myMarker = googleMap.addMarker(options);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10f) ) 。
} else {
myMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude() )。)
}
}
Bundle b=new Bundle()。
ArrayList<Platform> platforms= (ArrayList<Platform>) b.getSerializable("ListPlatforms"/span>)。
for(Platform platform:places) {
double latitudine = platform.getClatitudine__wgs84__()。
double longitudine = platform.getClongitudine__wgs_84__() 。
double distance = getDistanceinKm(latitudine, longitudine, location.getLatitude(), location.getLongitude())。
if (distance >=0.0) {
MarkerOptions options = new MarkerOptions()。
options.title(platform.getCdenominazione__())。
options.position(new LatLng(latitudine, longitudine))。
myMarker = googleMap.addMarker(options);
}
} ''' 但是我有java.lang.NullPointerException。Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference error when I try to do" for(Platform platform:places)". platforms in null. ...為什么?
uj5u.com熱心網友回復:
由于你沒有分享你如何在片段之間導航,我將假設你使用導航圖與行動。
因此,如果我們假設這是你的代碼,從片段A導航到B
NavHostFragment.findNavController(FragA.this)
.navigate(R.id.action_FragA_FragB); //navigate from FragA to FragB action。
- 然后你應該把你用過的bundle
b.putSerializable("ListPlatforms", (Serializable) data);從第一個片段上作為導航代碼的第二個引數,就像這樣(注意我現在把b作為方法的第二個引數):
NavHostFragment.findNavController(FragA.this)
.navigate(R.id.action_FragA_FragB,b); //navigate from FragA to FragB action。
- 那么在第二個片段中,你應該使用
getArguments()方法獲得這個b包,這個方法可以在片段的任何地方呼叫。
Bundle FragA_b = getArguments()。
ArrayList<Platform> platforms= (ArrayList<Platform>) FragA_b.getSerializable("ListPlatforms"/span>)。
N. B : 不要和savedInstanceState bundle混淆,它在OnCreate,onViewCreated,onStateRestored和onSaveState生命周期片段方法中作為引數被發送,這些savedInstanceState bundle與你在導航期間發送并使用getArguments()方法獲得的那個沒有關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/327616.html
標籤:
上一篇:Flutter構建方法在變數完全初始化之前就被呼叫,導致應用程式崩潰
下一篇:maven-構建時跳過依賴性
