我已经用RecyclerView实现了SearchView,但出现错误。 我正在开发android weather app,我想实现功能,当用户启动应用程序时,用户可以在用户点击名称时搜索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>
在我已实现Searchview的search.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>
答案 0 :(得分:0)
如果在本教程中看到的是“ SearchView”,而不是“ android.support.v7.widget.SearchView”。将其更改为“ SearchView”。
答案 1 :(得分:0)
这是一个简单的空指针异常。您没有共享全部错误,但是在错误结束时,您获得了错误来源所在的行号。只需单击该行,编译器就会自动定向到该行。
确保没有初始化就没有使用任何变量。
写list<Weather> = new ArrayList()
,而不仅仅是写list<Weather>
。