如何解决void'android.widget.EditText.addTextChangedListener(android.text.TextWatcher)'

时间:2016-01-18 17:44:43

标签: java android android-edittext textwatcher

我有一个片段,我正在获取用户输入,然后将输入传递给另一个方法,我使用EditText来获取用户的输入。我试图在线寻找解决方案,但似乎没有合适的答案来解决这个错误。

  

void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)

片段

public class FragmentSearch extends Fragment implements SearchMovieListener, SortListener {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    private static EditText mInputText;
    private Button searchButton;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;
    private ArrayList<Movie> mlistMovies = new ArrayList<>();
    private DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
    private RecyclerView listMovieHits;
    private AdapterBoxOffice mAdapterBoxOffice;
    private TextView textVolleyError;
    private MovieSorter movieSorter;
    private static final String STATE_MOVIES = "state_movies";
    // Search intialization
    Toolbar mToolbarF;

    public FragmentSearch() {
        // Required empty public constructor
        movieSorter = new MovieSorter();
    }

    public static FragmentSearch newInstance(String param1, String param2) {
        FragmentSearch fragment = new FragmentSearch();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        volleySingleton = VolleySingleton.getsInstance();
        requestQueue = volleySingleton.getmRequestQueue();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutSearch = inflater.inflate(R.layout.fragment_search, container, false);

        mInputText = (EditText) layoutSearch.findViewById(R.id.edt_tool_search);
        ImageView mImageView= (ImageView) layoutSearch.findViewById(R.id.img_tool_mic);

        textVolleyError = (TextView) layoutSearch.findViewById(R.id.textVolleyError);
        listMovieHits = (RecyclerView) layoutSearch.findViewById(R.id.search_movies);
        listMovieHits.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapterBoxOffice = new AdapterBoxOffice(getActivity());
        listMovieHits.setAdapter(mAdapterBoxOffice);
        if (savedInstanceState != null) {
            mlistMovies = savedInstanceState.getParcelableArrayList(STATE_MOVIES);
        } else {
            L.t(getActivity(), "executing task from the fragment");
            new TaskLoadSearchMovies(this).execute();
        }

        mAdapterBoxOffice.setMovieList(mlistMovies);
        return layoutSearch;
    }

    public static String searchMovies() {
        mInputText.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) {
                    /*L.E("I searched", mInputText.getText().toString());*/
                    /*L.t( ge, mInputText.getText().toString());*/
            }

        });
        return mInputText.getText().toString();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //save the movie list to a parcelable prior to rotation or configuration change
        outState.putParcelableArrayList(STATE_MOVIES, mlistMovies);
    }

    // Search <code></code>

    @Override
    public void onSortByName() {
        L.t(getActivity(), ("Sort name was clicked"));
        movieSorter.sortMovieByName(mlistMovies);
        mAdapterBoxOffice.notifyDataSetChanged();
    }

    @Override
    public void onSortByDate() {
        movieSorter.sortMoviesByDate(mlistMovies);
        mAdapterBoxOffice.notifyDataSetChanged();
    }

    @Override
    public void onSortByRatings() {
        movieSorter.sortMoviesByRating(mlistMovies);
        mAdapterBoxOffice.notifyDataSetChanged();
    }

    @Override
    public void onSearchMoviesLoaded(ArrayList<Movie> listMovies) {
        mAdapterBoxOffice.setMovieList(listMovies);
    }
}

logcat的

01-18 20:25:23.334 18697-18697/? I/art: Not late-enabling -Xcheck:jni (already on)
01-18 20:25:23.419 18697-18697/? W/System: ClassLoader referenced unknown path: /data/app/comeagain.materialdesign-2/lib/x86
01-18 20:25:23.931 18697-18697/comeagain.materialdesign I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
01-18 20:25:24.000 18697-18697/comeagain.materialdesign D/Dominic: onInterceptTouchEvent 
01-18 20:25:24.249 18697-18733/comeagain.materialdesign D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-18 20:25:24.316 18697-18697/comeagain.materialdesign D/VIVZ: loading entries 36Mon Jan 18 20:25:24 GMT+03:00 2016
01-18 20:25:24.319 18697-18697/comeagain.materialdesign D/VIVZ: loading entries 11Mon Jan 18 20:25:24 GMT+03:00 2016
01-18 20:25:24.383 18697-18733/comeagain.materialdesign I/OpenGLRenderer: Initialized EGL, version 1.4
01-18 20:25:24.438 18697-18733/comeagain.materialdesign W/EGL_emulation: eglSurfaceAttrib not implemented
01-18 20:25:24.438 18697-18733/comeagain.materialdesign W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebec20, error=EGL_SUCCESS
01-18 20:25:24.525 18697-18697/comeagain.materialdesign W/View: requestLayout() improperly called by android.support.v7.widget.AppCompatImageView{fb99aa9 V.ED..... ......ID 0,0-0,0 #7f0b009f app:id/selector} during layout: running second layout pass
01-18 20:25:24.525 18697-18697/comeagain.materialdesign W/View: requestLayout() improperly called by android.support.v7.widget.AppCompatImageView{6dc882e V.ED..... ......I. 0,0-0,0 #7f0b009f app:id/selector} during layout: running second layout pass
01-18 20:25:24.525 18697-18697/comeagain.materialdesign W/View: requestLayout() improperly called by android.support.v7.widget.AppCompatImageView{ef82fcf V.ED..... ......I. 0,0-0,0 #7f0b009f app:id/selector} during layout: running second layout pass
01-18 20:26:26.147 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.130ms
01-18 20:28:55.330 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.190ms
01-18 20:30:44.083 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 6.154ms
01-18 20:31:30.197 18697-23852/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: comeagain.materialdesign, PID: 18697
    java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:309)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference
      at comeagain.materialdesign.fragments.FragmentSearch.searchMovies(FragmentSearch.java:151)
      at comeagain.materialdesign.json.Endpoints.getRequestUrlSearchMovies(Endpoints.java:40)
      at comeagain.materialdesign.extras.MovieUtils.loadSearchMovies(MovieUtils.java:33)
      at comeagain.materialdesign.task.TaskLoadSearchMovies.doInBackground(TaskLoadSearchMovies.java:30)
      at comeagain.materialdesign.task.TaskLoadSearchMovies.doInBackground(TaskLoadSearchMovies.java:17)
      at android.os.AsyncTask$2.call(AsyncTask.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 
01-18 20:31:30.467 18697-18733/comeagain.materialdesign W/EGL_emulation: eglSurfaceAttrib not implemented
01-18 20:31:30.467 18697-18733/comeagain.materialdesign W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad72c640, error=EGL_SUCCESS
01-18 20:31:30.663 18697-18733/comeagain.materialdesign E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40162f0
01-18 20:31:32.206 18697-18733/comeagain.materialdesign E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb40172b0
01-18 20:32:34.724 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.065ms
01-18 20:34:29.454 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 24.052ms
01-18 20:34:41.965 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.668ms
01-18 20:35:16.054 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.501ms
01-18 20:36:03.117 18697-18703/comeagain.materialdesign W/art: Suspending all threads took: 5.564ms
01-18 20:36:30.316 18697-23852/? I/Process: Sending signal. PID: 18697 SIG: 9

我想为什么我会收到此错误以及如何解决?

0 个答案:

没有答案