viewpager中的片段发生错误

时间:2017-05-30 01:21:15

标签: android android-fragments nullpointerexception android-viewpager

嗨,我是一名新手程序员。制作一个Android应用程序,当我测试我自己的应用程序时,我遇到了错误。我已经制作了过滤文本来搜索数据库中的关键字。过滤器文本工作得很好,但AfterTextChanged方法在viewpager的片段中产生空指针异常错误。

java.lang.NullPointerException
ek.cert4u.Study$1.afterTextChanged(Study.java:196)
android.widget.TextView.sendAfterTextChanged(TextView.java:7424)
android.widget.TextView.setText(TextView.java:3822)
android.widget.TextView.setText(TextView.java:3671)
android.widget.EditText.setText(EditText.java:80)                                             android.widget.TextView.setText(TextView.java:3646)

如何从我的应用中删除这些错误?

这是ek.cert4u.Study的代码

public void onActivityCreated(Bundle b){
        super.onActivityCreated(b);
        NestedScrollView mScollView = (NestedScrollView) getView().findViewById(R.id.mScrollView4);
        MaterialViewPagerHelper.registerScrollView(getActivity(), mScollView);

        editText = (EditText) getView().findViewById(R.id.studyFilter);
        recommendedStudyListView = (ListView) getView().findViewById(R.id.StudyList);
        recommendedStudyListView.setTextFilterEnabled(true);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String filterText = s.toString();
                ((RecommendedStudyListAdapter) recommendedStudyListView.getAdapter()).getFilter().filter(filterText);
            }
        });

3 个答案:

答案 0 :(得分:0)

您还没有在recommendedStudyListView上设置适配器,因此getAdapter()返回null

答案 1 :(得分:0)

使用six代替onViewCreated

onActivityCreated
我猜这个

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.yourlayout, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    NestedScrollView mScollView = (NestedScrollView) view.findViewById(R.id.mScrollView4);
    MaterialViewPagerHelper.registerScrollView(getActivity(), mScollView);

    editText = (EditText) view.findViewById(R.id.studyFilter);
    recommendedStudyListView = (ListView) view.findViewById(R.id.StudyList);
    recommendedStudyListView.setTextFilterEnabled(true);

    RecommendedStudyListAdapter adapter = new RecommendedStudyListAdapter(.............);
    recommendedStudyListView.setAdapter(adapter);

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String filterText = s.toString();
            ((RecommendedStudyListAdapter) recommendedStudyListView.getAdapter()).getFilter().filter(filterText);
        }
    });

}

使你的片段崩溃,因为你没有在你的片段中设置任何适配器

答案 2 :(得分:0)

根据片段的生命周期,我们应该使用

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.yourlayout, container, false);
}

用于所有视图初始化。