如果从父活动调用,Fragment中的Butterknife视图是空引用

时间:2016-01-20 02:04:45

标签: android android-fragments butterknife

我使用Butterknife libray时遇到问题。我有SearchActivity.class在FragmentSearchAdapter中使用ViewPager和3 Fragment:

SearchActivity.class

'xirr(D3:D4,B3:B4)

FragmentSearchAdapter.class

mAdapter = new FragmentSearchAdapter(getSupportFragmentManager(), SearchActivity.this);
    mAdapter.addFragment("search histori");
    mAdapter.addFragment("search query");
    mAdapter.addFragment("search result");
        mPager.setAdapter(mAdapter);

    mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        public void onPageSelected(int currentIndex) {
            if(currentIndex==2) {
                SearchResultFragment SearchResultFragment = (SearchResultFragment) mAdapter.getItem(2);

                if (SearchResultFragment != null) {
                    SearchResultFragment.setDescription("Text 1","Text 2");
                }
          }
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) { }

        public void onPageScrollStateChanged(int arg0) { }
    });

其中一个片段是 SearchResultFragment.Class

 @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                SearchHistoryFragment search_histori = new SearchHistoryFragment();
                return search_histori;
            case 1:
                SearchQueryFragment search_query = new SearchQueryFragment();
                return search_query;
            case 2:
                SearchResultFragment search_result =  new SearchResultFragment();
                return search_result;
        }
        return null;
    }

我的问题:当从SearchActivity调用方法public class SearchResultFragment extends Fragment { @Bind(R.id.text1) TextView text1; @Bind(R.id.text2) TextView text2; private String page_index; private PageAlquranActivity activity; public SearchResultFragment() { } public static SearchResultFragment newInstance(int page_index) { SearchResultFragment fragment = new SearchResultFragment(); Bundle args = new Bundle(); args.putString(Variabel.page_index, String.valueOf(page_index)); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { page_index = getArguments().getString(Variabel.page_index); } activity = (PageAlquranActivity) getActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_search_result, container, false); ButterKnife.bind(this, v); return v; } public void setDescription(String s1, String s2) { text1.setText(s1); text2.setText(s2); } @Override public void onAttach(Context context) {super.onAttach(context);} @Override public void onDetach() { super.onDetach(); } } 时,我收到错误:

  

null对象引用

每个TextView。如果使用ButterKnife会出现此错误,但如果不是,如果我声明View这样就可以了:

SearchResultFragment.setDescription("Text 1","Text 2");
在onCreateView中

那怎么解决呢?对不起我的英语不好。感谢。

2 个答案:

答案 0 :(得分:0)

@AmayDiam你需要在被调用的方法中再次绑定ButterKnife。传递绑定的依赖关系 - 来自OnPageChangeListener的片段视图

<强> SearchActivity.class

ProgressBack

<强> SearchResultFragment.Class

mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int currentIndex) 
{
    if(currentIndex==2) 
    {
        SearchResultFragment SearchResultFragment = (SearchResultFragment) mAdapter.getItem(2);

        if (SearchResultFragment != null) 
        {
            SearchResultFragment.setDescription(mPager.getChildAt(currentIndex)"Text 1","Text 2");
        }
      }
    }

    public void onPageScrolled(int arg0, float arg1, int arg2) { }

    public void onPageScrollStateChanged(int arg0) { }
});

答案 1 :(得分:0)

也有类似的问题,因为我已经从Android Studio的依赖管理中添加了butterknife,而不是通过Butterknife网站的复制粘贴gradle行。 所以我不得不补充一下 compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' 而不仅仅是 compile 'com.jakewharton:butterknife:8.5.1'