我用recyclerview实现了searchview,但是我收到了错误?

问题描述 投票:1回答:2

我用RecyclerView实现了SearchView,但是我收到了一个错误。我正在开发android天气应用程序,我想实现功能,当用户启动应用程序用户可以搜索cityName时,用户点击名称它应该显示cityName和天气信息用户也可以添加城市添加和删除它但我已经按照本教程进行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)

我跟着this link但没有解决问题。

在我的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>
android android-recyclerview searchview
2个回答
0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.