Android-RecyclerView:未连接适配器;跳过布局错误

时间:2018-08-05 00:26:50

标签: android android-recyclerview adapter

我正在编写一个通过接收Json文件的API显示Boxoffice等级的应用程序 如果我评论有关Recyclerview的代码,一切都会正常。 适配器代码很好,我想..................................................... ................................................... ................................................... ...... 如果没有,logcat会这样说:

08-03 09:53:59.915 8189-8189/kr.ac.mju.mju_alimi E/RecyclerView: No adapter attached; skipping layout
08-03 09:54:00.351 8189-8189/kr.ac.mju.mju_alimi E/AndroidRuntime: FATAL EXCEPTION: main
    Process: kr.ac.mju.mju_alimi, PID: 8189
    java.lang.NullPointerException: Attempt to get length of null array
        at kr.ac.mju.mju_alimi.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:152)
        at kr.ac.mju.mju_alimi.MainActivity$MyAsyncTask.onPostExecute(MainActivity.java:97)

我认为代码的顺序是错误的,但不完全知道... MainActivity.java是...:

public class MainActivity extends AppCompatActivity {


    public static final String TAG = "MainActivity";
    TextView tv_result;

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    ArrayList<MovieItem> itemArrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //array list
        itemArrayList = new ArrayList<>(); //데이터

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);

//        tv_result = (TextView) findViewById(R.id.tv_result);
//        tv_result.setMovementMethod(new ScrollingMovementMethod()); //스크롤기능 추가하기

        MyAsyncTask mProcessTask = new MyAsyncTask();
        mProcessTask.execute();


        android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toolbar.findViewById(R.id.menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu pop = new PopupMenu(MainActivity.this, v);
                pop.getMenuInflater().inflate(R.menu.menu, pop.getMenu());
                pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        return false;
                    }
                });
                pop.show();
            }

        });

        ImageButton b = (ImageButton) findViewById(R.id.bell);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Pop.class));
            }
        });

    }


    //AsyncTask 생성 - 모든 네트워크 로직을 여기서 작성해 준다.
    public class MyAsyncTask extends AsyncTask<String, Void, MovieItem[]> {
        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

        //OkHttp 객체생성
        OkHttpClient client = new OkHttpClient();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setMessage("\t로딩중...");
            //show dialog
            progressDialog.show();
        }

        @Override
        protected MovieItem[] doInBackground(String... params) {

            HttpUrl.Builder urlBuilder = HttpUrl.parse("http://www.kobis.or.kr/kobisopenapi/webservice/rest/boxoffice/searchDailyBoxOfficeList.json").newBuilder();
            urlBuilder.addQueryParameter("key","430156241533f1d058c603178cc3ca0e");
            urlBuilder.addQueryParameter("targetDt","20170908");
            String url = urlBuilder.build().toString();

            Request request = new Request.Builder()
                    .url(url)
                    .build();

            try {
                Response response = client.newCall(request).execute();

                Gson gson = new GsonBuilder().create();
                JsonParser parser = new JsonParser();


                JsonElement rootObject = parser.parse(response.body().charStream())
                        .getAsJsonObject().get("boxOfficeResult").getAsJsonObject().get("dailyBoxOfficeList"); 
                MovieItem[] posts = gson.fromJson(rootObject, MovieItem[].class);

                return posts;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(MovieItem[] result) {
            super.onPostExecute(result);
            progressDialog.dismiss();

            if(result.length > 0){
                for (MovieItem post: result){
                    itemArrayList.add(post);

                }
            }

            mAdapter = new Adapter(itemArrayList);
            mRecyclerView.setAdapter(mAdapter);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您正在在调用异步任务之后设置适配器。这意味着,每次调用异步任务时,回收者视图的适配器都会更改。这是不必要的,因为您仅使用单个适配器。最好的方法是初始化适配器onCreate,然后在更改列表的值时通知适配器其数据集已更改。


TL; DR

将此代码移至创建时:

mAdapter = new Adapter(itemArrayList);
mRecyclerView.setAdapter(mAdapter);

并从代码的原始位置替换它( PS使用intellisense来确定那是否是正确的功能,关键字为 notify dataset 和< strong>已更改 ):

mAdapter.notifyDataSetChanged()