Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在分页库中实现重试android错误-空指针异常_Java_Android_Android Studio_Mvvm_Pagination - Fatal编程技术网

Java 在分页库中实现重试android错误-空指针异常

Java 在分页库中实现重试android错误-空指针异常,java,android,android-studio,mvvm,pagination,Java,Android,Android Studio,Mvvm,Pagination,我试图在使用android分页库时实现“重试”机制,以防在加载项目时没有互联网或设备脱机 当我单击“重试”映像时,我得到错误-“尝试在空对象引用上调用虚拟方法'void com.myapp.myapp.repository.FeedDataSource.retryPagination()”” FeedDataSource.java public class FeedDataSource extends PageKeyedDataSource<Long, Feed> { ..... /

我试图在使用android分页库时实现“重试”机制,以防在加载项目时没有互联网或设备脱机

当我单击“重试”映像时,我得到错误-“尝试在空对象引用上调用虚拟方法'void com.myapp.myapp.repository.FeedDataSource.retryPagination()”

FeedDataSource.java

public class FeedDataSource extends PageKeyedDataSource<Long, Feed> {
.....
//for retry
private LoadParams<Long> params;
private LoadCallback<Long, Feed> callback;
........
 @Override
public void loadAfter(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Long, Feed> callback) {

    this.params = params;
    this.callback = callback;

    Log.e(TAG, "Loading rage" + params.key + " Count" + params.requestedLoadSize);
    networkState.postValue(NetworkState.LOADING);
    RestApi restApi = RetrofitApi.create();
    Call<FeedResponse> call = restApi.fetchFeed(params.key, params.requestedLoadSize, username);
    try
    {
        Response<FeedResponse> response = call.execute();
        if(response.isSuccessful()){
            FeedResponse feedResponse = response.body();
            if(feedResponse !=null){
                networkState.postValue(NetworkState.LOADED);
                List<Feed> responseItems = feedResponse.getFeeds();
                callback.onResult(responseItems, params.key + 1);
            }
      }
         ........
 }

  public void retryPagination()
{
    loadAfter(params, callback);
}

 }
public class FeedDataFactory extends DataSource.Factory {

private MutableLiveData<FeedDataSource> mutableLiveData;
private FeedDataSource feedDataSource;
private AppController appController;

public FeedDataFactory(AppController appController){
    this.appController = appController;
    this.mutableLiveData = new MutableLiveData<FeedDataSource>();
}

@Override
public DataSource create() {
    feedDataSource = new FeedDataSource(appController);
    mutableLiveData.postValue(feedDataSource);
    return feedDataSource;
}

public MutableLiveData<FeedDataSource> getMutableLiveData(){
    return mutableLiveData;
   }
}
public class FeedViewModel extends ViewModel {

..............

public void retry() {
    FeedDataFactory feedDataFactory = new FeedDataFactory(appController);
    feedDataFactory.getMutableLiveData().getValue().retryPagination();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
  } 
public class FeedListAdapter extends PagedListAdapter<Feed, RecyclerView.ViewHolder> {
    ......
    private final Callback callback;

     public FeedListAdapter(@NonNull DiffUtil.ItemCallback<Feed> diffCallback, Callback callback, Context context) {
    super(Feed.DIFF_CALLBACK);
    this.context = context;
    this.callback = callback;
     }

    public void bindView(NetworkState networkState) {

            if (networkState != null && networkState.getStatus() == NetworkState.Status.FAILED) {
            binding.refresh.setVisibility(View.VISIBLE);
            binding.refresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.refresh.setVisibility(View.GONE);
                    binding.progressBar.setVisibility(View.VISIBLE);
                    callback.onRetryClicked();
                }
            });
        } 
    }
     public interface Callback {
      void onRetryClicked();
      }
  }
public class HomeFragment extends Fragment implements ClickHandler, FeedListAdapter.Callback {
  private FeedListAdapter feedListAdapter;
  private FeedViewModel feedViewModel;
   ..............
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        feedViewModel = new ViewModelProvider(this).get(FeedViewModel.class);
       feedViewModel.init(appController);

        feedListAdapter = new FeedListAdapter(Feed.DIFF_CALLBACK, this, getContext());

        recyclerView.setAdapter(feedListAdapter);

        feedViewModel.getArticleLiveData().observe(getViewLifecycleOwner(), pagedList -> {
        feedListAdapter.submitList(pagedList);
      });
      }
  }
FeedAdapter.java

public class FeedDataSource extends PageKeyedDataSource<Long, Feed> {
.....
//for retry
private LoadParams<Long> params;
private LoadCallback<Long, Feed> callback;
........
 @Override
public void loadAfter(@NonNull LoadParams<Long> params, @NonNull LoadCallback<Long, Feed> callback) {

    this.params = params;
    this.callback = callback;

    Log.e(TAG, "Loading rage" + params.key + " Count" + params.requestedLoadSize);
    networkState.postValue(NetworkState.LOADING);
    RestApi restApi = RetrofitApi.create();
    Call<FeedResponse> call = restApi.fetchFeed(params.key, params.requestedLoadSize, username);
    try
    {
        Response<FeedResponse> response = call.execute();
        if(response.isSuccessful()){
            FeedResponse feedResponse = response.body();
            if(feedResponse !=null){
                networkState.postValue(NetworkState.LOADED);
                List<Feed> responseItems = feedResponse.getFeeds();
                callback.onResult(responseItems, params.key + 1);
            }
      }
         ........
 }

  public void retryPagination()
{
    loadAfter(params, callback);
}

 }
public class FeedDataFactory extends DataSource.Factory {

private MutableLiveData<FeedDataSource> mutableLiveData;
private FeedDataSource feedDataSource;
private AppController appController;

public FeedDataFactory(AppController appController){
    this.appController = appController;
    this.mutableLiveData = new MutableLiveData<FeedDataSource>();
}

@Override
public DataSource create() {
    feedDataSource = new FeedDataSource(appController);
    mutableLiveData.postValue(feedDataSource);
    return feedDataSource;
}

public MutableLiveData<FeedDataSource> getMutableLiveData(){
    return mutableLiveData;
   }
}
public class FeedViewModel extends ViewModel {

..............

public void retry() {
    FeedDataFactory feedDataFactory = new FeedDataFactory(appController);
    feedDataFactory.getMutableLiveData().getValue().retryPagination();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
  } 
public class FeedListAdapter extends PagedListAdapter<Feed, RecyclerView.ViewHolder> {
    ......
    private final Callback callback;

     public FeedListAdapter(@NonNull DiffUtil.ItemCallback<Feed> diffCallback, Callback callback, Context context) {
    super(Feed.DIFF_CALLBACK);
    this.context = context;
    this.callback = callback;
     }

    public void bindView(NetworkState networkState) {

            if (networkState != null && networkState.getStatus() == NetworkState.Status.FAILED) {
            binding.refresh.setVisibility(View.VISIBLE);
            binding.refresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    binding.refresh.setVisibility(View.GONE);
                    binding.progressBar.setVisibility(View.VISIBLE);
                    callback.onRetryClicked();
                }
            });
        } 
    }
     public interface Callback {
      void onRetryClicked();
      }
  }
public class HomeFragment extends Fragment implements ClickHandler, FeedListAdapter.Callback {
  private FeedListAdapter feedListAdapter;
  private FeedViewModel feedViewModel;
   ..............
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        feedViewModel = new ViewModelProvider(this).get(FeedViewModel.class);
       feedViewModel.init(appController);

        feedListAdapter = new FeedListAdapter(Feed.DIFF_CALLBACK, this, getContext());

        recyclerView.setAdapter(feedListAdapter);

        feedViewModel.getArticleLiveData().observe(getViewLifecycleOwner(), pagedList -> {
        feedListAdapter.submitList(pagedList);
      });
      }
  }

请帮帮我,我做错了什么

首先,我要感谢您,因为您的解决方案是我实现网络重试的一个提示,我非常感谢

其次,关于您得到的NullPointerException,这是因为在FeedViewModel.java中,当触发重试按钮时,您为datasource创建了一个全新的工厂。这既不必要也不正确,因为工厂不会创建任何新的数据源,除非前一个数据源无效。您应该通过当前工厂检索数据源

我想提到的另一个问题是,如果您的请求失败,您不会在回调中调用onResult(…)。根据文件:

回调只能调用一次,如果再次调用将抛出。信息技术 对于接受回调的数据源加载方法始终有效 隐藏回调并稍后调用。这使数据源能够 完全异步,并处理临时的、可恢复的错误 状态(例如可以重试的网络错误)

以下是我在改型中调用onFailure()时在DataSource中的实现:

@Override
        public void onFailure(Call<HotItemsResponse> call, Throwable t) {
            loadingState.postValue(Contants.LoadingState.SUB_LOAD_ERROR);
            //when the load is fail, dont call onResult() on the call back,
            //just ignore it, update the loading state for the UI to handle reload

            //callback.onResult(new ArrayList<ProductItem>(), currentPage);
        }
@覆盖
失败时公共无效(调用调用,可丢弃的t){
loadingState.postValue(Contants.loadingState.SUB\u加载错误);
//加载失败时,不要在回调时调用onResult(),
//忽略它,更新UI的加载状态以处理重载
//onResult(新的ArrayList(),currentPage);
}

首先,我要感谢您,因为您的解决方案是我实现网络重试的一个提示,我非常感谢

其次,关于您得到的NullPointerException,这是因为在FeedViewModel.java中,当触发重试按钮时,您为datasource创建了一个全新的工厂。这既不必要也不正确,因为工厂不会创建任何新的数据源,除非前一个数据源无效。您应该通过当前工厂检索数据源

我想提到的另一个问题是,如果您的请求失败,您不会在回调中调用onResult(…)。根据文件:

回调只能调用一次,如果再次调用将抛出。信息技术 对于接受回调的数据源加载方法始终有效 隐藏回调并稍后调用。这使数据源能够 完全异步,并处理临时的、可恢复的错误 状态(例如可以重试的网络错误)

以下是我在改型中调用onFailure()时在DataSource中的实现:

@Override
        public void onFailure(Call<HotItemsResponse> call, Throwable t) {
            loadingState.postValue(Contants.LoadingState.SUB_LOAD_ERROR);
            //when the load is fail, dont call onResult() on the call back,
            //just ignore it, update the loading state for the UI to handle reload

            //callback.onResult(new ArrayList<ProductItem>(), currentPage);
        }
@覆盖
失败时公共无效(调用调用,可丢弃的t){
loadingState.postValue(Contants.loadingState.SUB\u加载错误);
//加载失败时,不要在回调时调用onResult(),
//忽略它,更新UI的加载状态以处理重载
//onResult(新的ArrayList(),currentPage);
}