Recycleview更新onCreateView我该怎么办

时间:2020-05-05 02:48:26

标签: android android-recyclerview

我是Android开发的初学者,如果用户看到F1(请参阅数据),然后转到F2(更改数据)并返回F1(此数据是旧数据),则不是用户表格F2更改数据< br /> 所以我想再次提供F1数据

错误消息

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

我认为可能是我的Rel没有初始化 我不知道,请给我一个建议

tab1:

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

Fragment1

如果使用onLazyLoad是正确的,但是当我使用“通话”为空

public class Fragment1 extends BaseLazyFragment {

private String url = "http://211.22.XXX.XXX/xampp/";
private RecyclerView Re1;
private int page = 1,run=0;
private String user_id,phone,cookie;
private TextView text1,text2,text3;
private Context context;
private boolean isFirstLoad = false;
private RecyclerView.LayoutManager mLayoutManager;

@Override
public void onLazyLoad() {
    initData();
    isFirstLoad =true;
}
public void change(){
    Log.e("change",toString());
    initData();
    run =1;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fragment1, container, false);

    initView(view);
    context = view.getContext();
    if(isFirstLoad){
        initData();
    }
    return view;
}
private void initView(View view){
    Re1 = view.findViewById(R.id.Re1_tab1);
    GlobalVariable Account = (GlobalVariable)getActivity().getApplicationContext();
    user_id = Account.getUser_id();
    phone = Account.getPhone();
    cookie = Account.getCookie();

}

private void initData(){
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork()   // or .detectAll() for all detectable problems
            .penaltyLog()
            .build());
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects()
            .penaltyLog()
            .penaltyDeath()
            .build());
    Handler myHandler = new Handler();
    myHandler.postDelayed(runTimerStop,400);
    if(cookie != null) {
        myHandler.removeCallbacks(runTimerStop);
    }
}
private Runnable runTimerStop = new Runnable() {
    @Override
    public void run() {
        select(user_id);

       // dialog.dismiss();
    }
};

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

} 家 enter image description here F1 enter image description here

0 个答案:

没有答案