iT邦幫忙

0

第二次使用 setLayoutManager 出現空對象

home
||
tab1 tab2 tab3 tab4(Fragment + viewpager)
||
F1 F2 F3(tab1 + TabsPagerAdapter)

刷新Recycleview的資料 有高手發生過這種狀態嗎?
請教教我

1.進到畫面第一次讀取(tab1) ....正常
2.已從背景刪除重新回到頁面讀取(tab1到tab4再回來tab1)....正常
3.利用setUserVisibleHint呼叫F1的讀取函式(tab1到tab2再回到tab1使用"setUserVisibleHint")....失敗

Fragment1 fragment = new Fragment1();
fragment.change();

"setUserVisibleHint"我確定在F1的change有成功被呼叫

public void change(){
    initData();;
    run =1;
}

錯誤訊息
如果我沒看錯就是指setLayoutManager是空的但這不對啊 這樣我第一次不可能跑
我有試過先把它清空 可是好像不是這個原因 求高手相助

E/error: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference

程式碼

 private void select(String id){
        try {

            String r = DBphp.DBstr(id,cookie,url);
            JSONArray jsonArray = new JSONArray(r);
            ArrayList<Rel_list> items = new ArrayList<Rel_list>();

            for (int i=0;i<jsonArray.length();i++){
                JSONObject jsonData = jsonArray.getJSONObject(i);
                items.add(new Rel_list(jsonData.getString("id"),jsonData.getString("product_name"),jsonData.getString("specification")
                        ,jsonData.getString("selling_price"),jsonData.getString("operations_center"),jsonData.getString("inventory"),jsonData.getString("pictuture_id"),jsonData.getString("ANS")));
            }

            RelAdapter SA = new RelAdapter(getContext(),items);
            Re1.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
            Re1.setAdapter(SA);
            SA.setOnItemClickListener(new RelAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int i) {
                  //  byte buff[] = new byte[125*250];
                    ImageView im0 = view.findViewById(R.id.img_id);
                    TextView te0 = view.findViewById(R.id.t1);
                    TextView te1 = view.findViewById(R.id.t2);
                    TextView te2 = view.findViewById(R.id.t3);
                    TextView te3 = view.findViewById(R.id.t4);
                    TextView te4 = view.findViewById(R.id.t5);
                    TextView te5 = view.findViewById(R.id.t6);
                    TextView te6 = view.findViewById(R.id.t7);
                    TextView te7 = view.findViewById(R.id.t14);
              //      love = view.findViewById(R.id.btn_love);
                  //  Bitmap bitmap = setimage(im0); //调用setimage方法,得到返回值bitmap
                    Intent intent= new Intent();//轉跳另一頁,並顯示帳密及信箱
                    intent.setClass(getContext(), shop_thing.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("user_id",user_id);
                    bundle.putString("user_phone",phone);
                    bundle.putString("cookie",cookie);
                    bundle.putString("page", String.valueOf(page));
                    bundle.putString("shop_id",te0.getText().toString());
                    bundle.putString("shop_product_name",te1.getText().toString());
                    bundle.putString("shop_specification",te2.getText().toString());
                    bundle.putString("shop_selling_price",te3.getText().toString());
                    bundle.putString("shop_operations_center",te4.getText().toString());
                    bundle.putString("shop_inventory",te5.getText().toString());
                    bundle.putString("picture_id",te6.getText().toString());
                    bundle.putString("love",te7.getText().toString());

                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
        }catch (Exception e){
            Log.e("error",e.toString());
        }
    }
https://stackoverflow.com/questions/25667657/viewpager-call-setuservisiblehint-after-first-fragment-is-loaded
不知道這篇有沒有用

尚未有邦友回答

立即登入回答