小编Aks*_*hah的帖子

viewModelScope.launch 第二次不起作用

我有 2 个片段,FragmentA 包含星球大战角色列表,而 FragmentB 包含该角色的详细信息。我在片段中使用 viewModelScope.launch 来获取角色的详细信息。下面是我的视图模型

@HiltViewModel
class DetailsViewModel @Inject constructor(
    private val getSpecieDetailsUseCase: GetSpecieDetailsUseCase,
    private val getFilmDetailsUseCase: GetFilmDetailsUseCase,
    private val getPlanetDetailsUseCase: GetPlanetDetailsUseCase,
    private val mapper: CharacterDetailMapper
) : ViewModel() {

    var characterDetailsModel = MutableLiveData<CharacterDetailsModel?>()

    fun init(infoModel: CharacterInfoModel?) {
        characterDetailsModel.postValue(null)
        try {
            viewModelScope.launch {
                infoModel?.let {
                    val specieResponse = async(context = Dispatchers.IO) {
                        it.specieIdList?.map {
                            getSpecieDetailsUseCase.executeUseCase(
                                GetSpecieDetailsUseCase.GetSpecieDetailsRequest(
                                    it
                                )
                            )
                        }
                    }
                    val filmResponse = async(context = Dispatchers.IO) {
                        it.filmsIdList?.map {
                            getFilmDetailsUseCase.executeUseCase(
                                GetFilmDetailsUseCase.GetFilmDetailsRequest(
                                    it
                                )
                            )
                        }
                    } …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-navigation android-viewmodel kotlin-coroutines

13
推荐指数
1
解决办法
2716
查看次数

尝试在空对象引用上调用虚方法'android.content.Context android.support.v4.app.FragmentActivity.getApplicationContext()'

以下是我的片段代码,我得到上述null异常错误

 /**
  * Created by USer on 09-04-2016.
  */
 public class FeaturedFragment extends android.support.v4.app.Fragment {
     private List url;
     private SliderLayout sliderLayout;
     private GridView gridView;
     private int firstVisiblepos;
     AppnextAPI api;

     @Nullable
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View rootView = inflater.inflate(
                 R.layout.featured, container, false);
         url = new ArrayList<String>();
         url.add("http://192.168.5.51/Wallpapers/Autumn.jpg");
         url.add("http://192.168.5.51/Wallpapers/Tree.jpg");
         url.add("http://192.168.5.51/Wallpapers/Flower.jpg");
         url.add("http://192.168.5.51/Wallpapers/Lion.jpg");
         api = new AppnextAPI(getActivity(), "my id");
         sliderLayout = (SliderLayout) rootView.findViewById(R.id.slider);
         gridView = (GridView) rootView.findViewById(R.id.gridView);
         ThemeShowcase theme = new ThemeShowcase();
         theme.execute("getThemes");
         for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

android nullpointerexception android-fragments baseadapter

6
推荐指数
1
解决办法
2万
查看次数