我已经使用 recyclerview 实现了 searchview,但出现错误?

I have implemented searchview using with recyclerview but I am getting error?

我已经使用 RecyclerView 实现了 SearchView,但出现错误。 我正在开发 android 天气应用程序,我想在用户启动应用程序时实现功能用户可以搜索城市名称当用户点击名称时它应该显示城市名称和天气信息用户也可以添加城市添加和删除它但我已经关注本教程适用于 SearchView searchviewtutorial

但是我收到这个错误

 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
                      at yodgobekkomilov.edgar.com.weather.MainActivity.onCreate(MainActivity.java:47)

我关注了但是没有解决问题

在我的 MainActivity.java 文件下面,我在其中实现了带有 Recyclerview 的 SearchView

    public class MainActivity extends AppCompatActivity {
        private List<Weather> weatherArrayList;
        private List<ForecastEndingPoint> conditionList;
        private ProgressDialog pDialog;
        private RecyclerView recyclerView;
        private SearchView mSearchView;
        private WeatherAdapter adapter;
        private ConditionAdapter conditionAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mSearchView = (SearchView) findViewById(R.id.searchView);
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Data.. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

            //Creating an object of our api interface

            mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    ApiService api = WeatherClient.getApiService();

                    /**
                     * Calling JSON
                     */
                    Call<List<Weather>> call = api.getWeather("6bed69052a864d44a8e165653183008", "Andijan");


                    /**
                     * Enqueue Callback will be call when get response...
                     */
                    call.enqueue(new Callback<List<Weather>>() {

                        @Override
                        public void onResponse(Call<List<Weather>> call, Response<List<Weather>> response) {
                            //Dismiss Dialog
                            //    Log.d("url", call.request().url().toString());
                            pDialog.dismiss();

                            if (response.isSuccessful()) {
                                /**
                                 * Got Successfully
                                 */
                                weatherArrayList = response.body();

                                recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

                                LinearLayoutManager eLayoutManager = null;

                                adapter = new WeatherAdapter((List<Weather>) weatherArrayList);
                                eLayoutManager = new LinearLayoutManager(getApplicationContext());
                                recyclerView.setLayoutManager(eLayoutManager);
                                recyclerView.setItemAnimator(new DefaultItemAnimator());
                                recyclerView.setAdapter(adapter);
                            }
                        }


                        @Override
                        public void onFailure(Call<List<Weather>> call, Throwable t) {
                            pDialog.dismiss();
                        }
                    });
                   getApi();

                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });

        }


        public void getApi(){
            ApiService apiService = WeatherClient.getApiService();
            Call<ForecastEndingPoint> conditionCall = apiService.forecast("6bed69052a864d44a8e165653183008", "Andijan", 5);
            conditionCall.enqueue(new Callback<ForecastEndingPoint>() {

                @Override
                public void onResponse(Call<ForecastEndingPoint> conditionCall, Response<ForecastEndingPoint> response) {
                    //Dismiss Dialog
                    //    Log.d("url", call.request().url().toString());
                    if (pDialog != null && pDialog.isShowing()) {
                        pDialog.dismiss();
                    }

                    if (response.isSuccessful()) {
                        /**
                         * Got Successfully
                         */
                        conditionList = Collections.singletonList(response.body());

                        recyclerView = findViewById(R.id.recycler_view);

                        LinearLayoutManager eLayoutManager = null;
                        Context context = getApplicationContext();
                        Context picassoContext = getApplicationContext();
                        conditionAdapter = new ConditionAdapter(conditionList, context, picassoContext);
                        eLayoutManager = new LinearLayoutManager(getApplicationContext());
                        recyclerView.setLayoutManager(eLayoutManager);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());
                        recyclerView.setAdapter(conditionAdapter);

                    }
                }

                @Override
                public void onFailure(Call<ForecastEndingPoint> conditionCall, Throwable t) {
                    pDialog.dismiss();
                }
            });
        }
        }

在我托管 recyclerview 的 activity_main.xml 文件下方。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view"
        android:fadeScrollbars="true"
        android:scrollbars="vertical"/>

    </RelativeLayout>

下面 search.xml 我已经实现了 Searchview

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:iconifiedByDefault="false"
            android:queryHint="Search Here"
            android:layout_centerHorizontal="true" />

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

如果您在教程中看到它是 "SearchView",而不是 "android.support.v7.widget.SearchView"。将其更改为 "SearchView".

这是一个简单的空指针异常。你没有分享整个错误,但在错误结束时你会得到一些错误出现的行号 from.just 单击它,编译器会自动定向到该行。 确保您没有在未初始化的情况下使用任何变量。 写 list<Weather> = new ArrayList() 而不是只写 list<Weather>.