게시글
질문&답변
2020.09.02
Store가 null이 나옵니다
찾았네요.. data클래스 Stores s가 대문자였네요..
- 0
- 3
- 122
질문&답변
2020.09.02
Store가 null이 나옵니다
class MainViewModel : ViewModel () { val itemLiveData = MutableLiveData List Store >>() val loadingLiveData = MutableLiveData Boolean >() private val service : MaskService init { val retrofit = Retrofit . Builder () . baseUrl ( MaskService . BASE_URL ) . addConverterFactory ( MoshiConverterFactory . create ()) . build () service = retrofit . create ( MaskService :: class . java ) fetchStoreInfo () } fun fetchStoreInfo () { loadingLiveData . value = true viewModelScope . launch { val storeInfo = service . fetchStoreInfo ( 37.188078 , 127.043002 ) Log . e ( "tag" , "info " + storeInfo . Stores ) itemLiveData . value = storeInfo . Stores loadingLiveData . value = false } } } 해당과같이 Mutable데LiveData로 초기화 하고있고 location은 강의대로 고정 위도경도 넣어주고있습니다 (강의에서처럼 코틀린에선 현재 위도경도 안써서 강의대로 안쓰고 저렇게 넣어놨습니다!) 저기 info로 로그찍은 곳은 null이나옵니다 (itemLiveData도 null 로 들어가겠네요..) storeInfo.count로 하는경우에는 count가 222로 잘나오며 storeInfo.Stores만 자꾸 null이 나옵니다
- 0
- 3
- 122
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
뷰모델 생성자에서 performAction 메소드를 호출하고 있어서 그랬던건 확인되서 해결을 했는데 if ( location != null ) { location . setLatitude ( 37.188078 ); location . setLongitude ( 127.043002 ); viewModel . location = location ; viewModel . fetchStoreInfo (); } 이렇게 setlocation해도 그 json에 있던거 그대로 파싱이되네요 현재위치론 안되고.. 샘플 json이라그런건가요?
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
넵 감사합니다
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
올려주신 깃헙에 main이랑 비교하고 performAction() 메소드부분은 복붙까지해봤는데도 여기먼저안타고 뷰모델을 타네요.. 제 올린 코드 한번 복붙해서 확인해주실수있나요?
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
리프레시버튼 안누르고 앱 진입하자마자 죽습니다 ㅠㅠ 그래서 fecthStoreInfo에서 널체크도 해봤는데 그냥 아무것도 안나오더라구요 fail 리스너도 넣어봤었는데 success로 탑니다
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
그래서 강의에 있는것처럼 setlocation으로 위도경도 줘도 viewmodel에서는 null이 찍힙니다
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
메인에 viewmodel.location = location 이부분이랑 service.fetchStoreInfo(location.getLatitude(), location.getLongitude()).enqueue(new Callback () { 이부분을 브레이크포인트 잡고 돌려보면 service.fetchStoreInfo(location.getLatitude(), location.getLongitude()).enqueue(new Callback () { 여기부분이 먼저 돌면서 null을 찍어서 앱이 죽고 viewmodel.location = location 여기는 아예 타지도 않더라구요.. 레트로핏 통신부분을 주석하면 location 들어간건 확인됩니다
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
public class MainViewModel extends ViewModel { public MutableLiveData List Store >> itemLiveData = new MutableLiveData (); public Location location ; private Retrofit retrofit = new Retrofit . Builder () . baseUrl ( MaskService . BASE_URL ) . addConverterFactory ( MoshiConverterFactory . create ()) . build (); private MaskService service = retrofit . create ( MaskService . class ); public MainViewModel (){ fetchStoreInfo (); } public void fetchStoreInfo () { service . fetchStoreInfo ( location . getLatitude (), location . getLongitude ()). enqueue ( new Callback StoreInfo >() { @Override public void onResponse ( Call StoreInfo > call , Response StoreInfo > response ) { List Store > items = response . body (). getStores () . stream () . filter ( item -> item . getRemainStat () != null ) . collect ( Collectors . toList ()); itemLiveData . postValue ( items ); } @Override public void onFailure ( Call StoreInfo > call , Throwable t ) { Log . e ( "tag" , "fail " , t ); itemLiveData . postValue ( Collections . emptyList ()); } }); } }
- 0
- 14
- 381
질문&답변
2020.08.30
실제 위도경도 가져올때 널포인터납니다
아직도 해결을 찾지 못해서 답글답니다 ㅠㅠ 전체소스를 올려보겠습니다 (메인, 뷰모델만요 어댑터는 따로 분리해서 메인에 없습니다) public class MainActivity extends AppCompatActivity { private MainViewModel viewModel ; private FusedLocationProviderClient fusedLocationClient ; @Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . layout . activity_main ); viewModel = new ViewModelProvider ( this ). get ( MainViewModel . class ); fusedLocationClient = LocationServices . getFusedLocationProviderClient ( this ); PermissionListener permissionlistener = new PermissionListener () { @Override public void onPermissionGranted () { performAction (); } @Override public void onPermissionDenied ( List String > deniedPermissions ) { } }; TedPermission . with ( this ) . setPermissionListener ( permissionlistener ) . setDeniedMessage ( "If you reject permission,you can not use this service \n\n Please turn on permissions at [Setting] > [Permission]" ) . setPermissions ( Manifest . permission . ACCESS_FINE_LOCATION ) . check (); } @SuppressLint ( "MissingPermission" ) private void performAction () { fusedLocationClient . getLastLocation () . addOnSuccessListener ( this , location -> { if ( location != null ) { Log . e ( "tag" , "location" + location . getLatitude ()); viewModel . location = location ; viewModel . fetchStoreInfo (); } }); RecyclerView recyclerView = findViewById ( R . id . recycler_view ); recyclerView . setLayoutManager ( new LinearLayoutManager ( this , RecyclerView . VERTICAL , false )); final StoreAdapter adapter = new StoreAdapter (); recyclerView . setAdapter ( adapter ); // ui 변경 감지 viewModel . itemLiveData . observe ( this , stores -> { adapter . updateItems ( stores ); getSupportActionBar (). setTitle ( " 마스크 재고 있는 곳 : " + stores . size ()); }); } @Override public boolean onCreateOptionsMenu ( Menu menu ) { MenuInflater inflater = getMenuInflater (); inflater . inflate ( R . menu . main_menu , menu ); return true ; } @Override public boolean onOptionsItemSelected ( MenuItem item ) { // Handle item selection switch ( item . getItemId ()) { case R . id . action_refresh : viewModel . fetchStoreInfo (); return true ; default : return super . onOptionsItemSelected ( item ); } } }
- 0
- 14
- 381