我的Recycler视图的自定义适配器不断崩溃,因为即使我已经初始化它也是null

时间:2016-02-29 16:55:25

标签: java android android-recyclerview recycler-adapter

这是我几天来一直遇到的问题,我尝试了很多解决方案。

使用我的最新解决方案,它一直在崩溃。

这是我的function popup(path) { var ajaxRequest; ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var e = document.getElementById('container'); e.onmouseover = function() { document.getElementById('descriptionbox').style.display = 'block'; } e.onmouseout = function() { document.getElementById('descriptionbox').style.display = 'none'; } } } ajaxRequest.open("GET", path, true); ajaxRequest.send(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,但如果我已初步化它,它怎么可能是friggin NULL

当我在另一个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)课程中的listadapter但仍然崩溃了

3 个答案:

答案 0 :(得分:0)

在填充列表后尝试设置RecyclerView的适配器。

答案 1 :(得分:0)

尝试将构造函数更改为 -

public PopulateUsers(ArrayList<UserInfo> list, UserAdapter adapter, RecyclerView recyclerView) {
    this.list = new ArrayList<UserInfo>(); 
    this.adapter = new UserAdapter();
    this.list = list;
    this.recyclerView = recyclerView;
}

答案 2 :(得分:0)

类似的东西:

list = new ArrayList<UserInfo>();
    list.add("your value");
    list.add("your another value");
    adapter = new MusicRecyclerAdapter(list);
    recyclerView.setAdapter(adapter);