1

我正在使用 MVP。我的活动包含一个片段。我正在初始化,然后将演示者设置为 Main-Activity 的 on Create 方法中的片段,如下所示。

public class MainActivity extends AppCompatActivity {

    private StashPresenter stashPresenter;
    private MainFragment mainFragment;
    FragmentManager fm;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    App.getInstance().getAppComponent().inject(this);
    setContentView(R.layout.activity_main);

    fm = getSupportFragmentManager();

    fm.beginTransaction()
    .add(R.id.fragment_container, MainFragment.newInstance(),"mainFragment")
    .commitNow();

    mainFragment = (MainFragment) fm.findFragmentById(R.id.fragment_container);

    stashPresenter = new StashPresenter(mainFragment);

    mainFragment.setPresenter(stashPresenter);
}

在我的 mainFrgament 类中,我是 setPresenterFunction 中的 settinf Presenter,如下所示。

public class MainFragment extends Fragment implements 
StashContract.PublishToView {

public StashContract.ToPresenter forwardInteraction;

public void setPresenter(StashContract.ToPresenter forwardInteraction)
{
    this.forwardInteraction = forwardInteraction;
}

有时,在 mainFragment 的 OnCreateView 中执行以下代码所示的搜索操作时,我收到一条错误消息,指出我的转发 “尝试在空对象引用上调用接口方法”

有时我会收到此错误,有时我不会。我不明白为什么会这样

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    unbinder = ButterKnife.bind(this, view);


    searchView.setOnEditorActionListener((v, actionId, event) -> {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            progressBar.setVisibility(View.VISIBLE);

            forwardInteraction.searchButtonClick(actionId, searchView.getText().toString());

            return true;
        }
        return false;
    });


    String[] artistNames = getResources().getStringArray(R.array.artistNamesSuggestion);
    ArrayAdapter<String> adapterArtist = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.fragment_main, R.id.search_phrase, artistNames);
    searchView.setAdapter(adapterArtist);
    searchView.setThreshold(1);

    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
    recyclerView.setAdapter(adapter);
    recyclerView.addItemDecoration(new SpaceItemDecoration(space, space, space, space));

    return view;
}
4

1 回答 1

0

Solution

In the "onCreateView" method of the fragment, only initialize the views.

Move rest of the code in the "onResume" method.

Reason for error

Check this image

enter image description here

As you can see, "onCreateView" of your fragment is called when you are in the "onCreate" method in you activity.

In the current state of your code, there maybe times you when you will try to use the presenter before it is initialized.

Therefore, set your presented in the "onCreate" method of your activity and use it either it on the "onStart" or "onResume" of your fragment.

You can check this project to understand the MVP architecture more.

于 2017-09-13T10:19:17.963 回答