点击按钮更新recyclelerview

时间:2016-03-02 01:13:36

标签: android android-recyclerview adapter

我的Android应用中还有其他问题或错误。对不起,我是Android开发的新手,但这是我的热情,我会坚持下去。

无论如何,我的代码在我的CustomAdapter类中的方法调用中崩溃。

这是我崩溃的CustomAdapter类:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
private ArrayList<UserInfo> arrayList = new ArrayList<UserInfo>();
float ratings;

public CustomAdapter(ArrayList<UserInfo> arrayList){
    this.arrayList = arrayList;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.userslayout, parent, false);
    CustomViewHolder customViewHolder = new CustomViewHolder(view);
    return customViewHolder;
}

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
    final UserInfo userInfo = arrayList.get(position);
    holder.username.setText(userInfo.getMusicName());
    holder.forename.setText(userInfo.getMusicCategory());
    holder.surname.setText(userInfo.getFileType());
    // THE APP CRASHES WHEN I CLICK THIS BUTTON. THINGS UPDATE IN THE 
    // DATABASE - BUT THE RECYCLERVIEW CRASHES AND THE APP CRASHES
    holder.buttonRefresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String pressed = Integer.toString(userInfo.getPressedName());
            UserListRecycler userListRecycler = new UserListRecycler();
            UserListRecycler.UserPressedAsync userPressedAsync = userListRecycler.new UserPressedAsync();
            userPressedAsync.execute(username, pressed);
        }
    });
}

}

这是我的UserListRecycler课程 - 主要课程和内部课程UserPressedAsync

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();
    }

    public class UserPressedAsync extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        // REST STUFF HAPPENS HERE
    }

    @Override
    protected void onPostExecute(String postData) {
        // CRASHES HERE!!!
        populateRecyclerList();
    }
}
}

这是我的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 {
        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);
        }
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

基本上,当点击按钮时,会在backend,即phpmyadmin mysql服务器中更新内容,但应用程序在崩溃时不会使用更新后的列表重新加载。这与我的populateList()类中的UserListRecycler方法有关,它将错误指向recyclerView.setAdapter(adapter);类中的PopulateUsers - 恰好位于onPostExecute()中,作为NullPointerException,但它在第一次实例化和加载UserListRecycler类时工作正常。那么,当我调用populateList()方法时,为什么会崩溃?

这是我的堆栈跟踪:

 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)

这真的意味着很多,事实上如果有人可以帮助我解决这个问题,我会为自己睡一觉。这是一个可怕的错误!

2 个答案:

答案 0 :(得分:2)

该错误主要来自于您正在访问您的recyclerview而其适配器尚未实例化的事实。这是因为您在主线程中显示recyclelerview,同时在后台设置它。在您的情况下,两个线程不同步,因此当您单击按钮时,Recyclerview尚未正确设置,从而为您提供空指针异常。

我建议你找到一个可以显示数据的工作,然后在显示之前设置你的recyclerview。

使用asynctask进行Web调用真的不是一个好主意,你通常必须使用诸如retrofit,gson,rxandroid等库,但是由于你刚刚开始,你必须使用asynctask。< / p>

我建议您在recyclerView.setAdapter(adapter)主线程中使用onCreate,因为您没有显示回收站视图所需的数据,您必须设置它虚拟数据首先,或者更优选地,不显示它,首先显示进度条以告知用户它尚未完成加载。

如果你按照我的建议行事,你就不必在你的asynctask中打电话给recyclerView.setAdapter(adapter),你只需通知它你有变化,这当然是你的adapter.notifyDataSetChanged()

答案 1 :(得分:0)

recyclerView.setAdapter(adapter);

你打电话给我两次。为什么不将它留在onCreateView中并将其从postExecute中删除?只需将adapter.notifyDataSetChanged()留在postExecute中,然后从onCreateView中删除它。它有所作为吗?

我正在使用库 Volley 来完成这些任务,并且它非常易于使用。看起来比ASyncTask还要整洁。也许你可以尝试一下。

查看该页面:http://developer.android.com/training/volley/index.html

如果您克隆Volley library(下载文件夹),请通过New>Import MODULE>path to library将其导入Android Studio。然后,您需要将compile project(':volley')放入应用程序的Build gradle依赖项下 - 所以它看起来像这样:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile project(':volley')
}

这是我从php服务下载JSON的方法如下:

  public void downloadEvents(String urlService) {


    RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); //getActivity because I'm calling it from a fragment, if called in Activity, use 'this'
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
            (Request.Method.GET, urlService, null, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {

                    try {


                        for (int i = 0; i < response.length(); i++) {
                            JSONObject eventJson = response.getJSONObject(i);

                            String title = eventJson.getString("EventTitle");
                            String body = eventJson.getString("EventBody");
                            double price = Double.parseDouble(eventJson.getString("EventPrice"));
                            String date = eventJson.getString("EventDate");
                            String time = eventJson.getString("EventTime");
                            String place = eventJson.getString("EventPlace");
                            String organ = eventJson.getString("Organization");

                            Event event = new Event(title, body, price, date, time, durationStr, place, organ);
                            theEvents.add(event);
                            rAdapter.notifyItemInserted(i);
                        }


                        if (swipeRefreshLayout.isRefreshing()) {
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }


            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY ERROR", "" + error);

                }
            }
            );

    requestQueue.add(jsonArrayRequest); //important to add that otherwise the download wont be initiated.
}

您可以看到我如何处理我的JSON以及我可以使用swipeRefreshLayout之类的内容。 请注意,这仍然是asynchronously

重要的是去检查你的JSON responses并查看响应是以对象还是数组开头的。然后相应地调整Volley代码。它基本上将代码中的大多数Array关键字更改为Object(如果您正在接收保存数据的JSON对象)。例如:

 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
            (Request.Method.GET, urlService, null, new Response.Listener<JSONArray>() {

变为

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest 
            (Request.Method.GET, urlService, null, new Response.Listener<JSONObject>() {

...

在我的onCreate中,在使用downloadEvents("http//example.com/service.php");之前 我已经完成了所有RecyclerView和适配器代码:

        recyclerView = (RecyclerView) fragmentView.findViewById(R.id.rv);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(llm);
    theEvents = new ArrayList<>();
    rAdapter = new RecyclerAdapter(theEvents, recyclerView);
    recyclerView.setAdapter(rAdapter);

下载方法只是通知适配器。