我已经使用RecyclerView实现了SearchView,但是我遇到了错误。 我正在开发 android 天气应用程序,我想在用户启动应用程序时实现功能,当用户点击名称时,用户可以搜索城市名称,它应该显示城市名称和天气信息,用户也可以添加城市添加和删除它,但我遵循了本教程的 SearchView 搜索视图教程
但是我收到此错误
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 文件下面,我已经在其中实现了带有回收器视图的搜索视图
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();
}
});
}
}
在我的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>
在搜索下方.xml我在其中实现了搜索视图
<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"。将其更改为"搜索视图"。
这是一个简单的空指针异常。 您没有共享整个错误,但在错误结束时,您会得到错误来源的一些行号。只需单击该行,编译器就会自动定向到该行。 确保您没有使用任何变量而不对其进行初始化。 写list<Weather> = new ArrayList()
而不仅仅是写list<Weather>
。