我在代码中发现了一个错误,recyclerview
可能是null
。我该如何解决这个问题?
这些是类和代码:
这是我的UserListRecycler
课程 - 主要课程
public class UserListRecycler extends Fragment {
RecyclerView recyclerView;
static UserAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<UserInfo> list;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.userlistGUI, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.reUsers);
recyclerView.setHasFixedSize(true);
list = new ArrayList<UserInfo>();
// Instantiate new adapter here
adapter = new MusicRecyclerAdapter(list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
// Sets the adapter here
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;
}
@Override
public void onStart() {
super.onStart();
populateRecyclerList();
}
public void populateList(){
PopulateUsers userList = new PopulateUsers(list, adapter, recyclerView);
userList.execute();
}
}
这是我的PopulateUsers
课程 - 我的服务器的详细信息是通过REST
public class PopulateUsers extends AsyncTask<String, String, String> {
static ArrayList<UserInfo> list;
UserAdapter adapter;
RecyclerView recyclerView;
public PopulateUsers(ArrayList<UserInfo> list, UserAdapter adapter, RecyclerView recyclerView) {
this.list = new ArrayList<UserInfo>();
this.adapter = new UserAdapter();
this.list = list;
this.adapter = adapter;
this.recyclerView = recyclerView;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// `REST` Activity happens here
}
@Override
protected void onPostExecute(String s) {
try {
// IT CRASHES HERE, BUT IF I INSTANTIATE A NEW LIST,
// THEN THIS ERROR GOES AND NOTHING IS DISPLAYED
list.clear();
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
String forename = jsonArray.getJSONObject(i).getString("forename");
String surname = jsonArray.getJSONObject(i).getString("surname");
String nationality = jsonArray.getJSONObject(i).getString("nationality");
UserInfo userInfo = new UserInfo(forename, surname, nationality);
list.add(userInfo);
}
// THIS LINE BELOW IS THE BIGGEST CULPRIT. IT HAS BEEN A HUGE HEADACHE
// I EVEN TRIED THIS: adapter = new MusicRecyclerAdapter(list);
// AND IT STILL CRASHES
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
adapter
是NULL
,但如果我已初步化它,它怎么可能是friggin NULL
!
当我在另一个populateList()
课程中调用AsyncTask
方法时,它会一直崩溃 - 请参阅下文:
public class RecommendUsers extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
// REST stuff happens here
}
@Override
protected void onPostExecute(String postData) {
// The following line invokes the instance of `PopulateUserList` and crashes.
// This is the culprit line
populateList();
}
}
这是stack trace
:
java.lang.NullPointerException
at lukasz.usersapp.PopulateUsers.onPostExecute(PopulateUsers.java:67)
at lukasz.usersapp.PopulateUsers.onPostExecute(PopulateUsers.java:27)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
基本上它在list.clear()
崩溃了,但这不是问题 - 我可以解决,但是跟进 - recyclerView.setAdapter(adapter)
让我头痛不已。字面意思是让我疯狂!!!“
希望得到一些支持或解决方案
在PopulateUsers
构造函数中,我正在初始化,但我觉得recyclerview
可能是NULL
因此我的代码崩溃了 - 请参阅下面的构造函数:
public PopulateUsers(ArrayList<UserInfo> list, UserAdapter adapter, RecyclerView recyclerView) {
this.list = new ArrayList<UserInfo>();
this.adapter = new UserAdapter();
this.list = list;
this.adapter = adapter;
this.recyclerView = recyclerView;
}