NullPointerException在Fragment中的我的小部件中

时间:2018-01-04 23:49:33

标签: java android null butterknife

当调用方法(Bill)时,我用Butterknife初始化的小部件返回null(noPrev和recyclerList)。

在其他类如活动中,我使用相同的方法(将小部件与BUtterknife绑定)并且它运行良好但是当我尝试在片段中绑定我的小部件时,它总是返回null。 .................................................. ......................

public class Home extends Fragment implements BridgeInterface.BillsResponse{


@BindView(R.id.rvBillList)
RecyclerView recyclerList;

@BindView(R.id.tvNoPreviousBill)
TextView noPrev;

public List<Bill> list_of_bill = new ArrayList<>();
private BillRecyclerAdapter adapter;
private Handler h;
private Runnable r;
private View v;

public Home() {
    // Required empty public constructor
}

public static Home getInstance() {
    Home home = new Home();
    return home;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(Home.this,v);
    if(list_of_bill.size()==0){
        noPrev.setVisibility(View.VISIBLE);
        recyclerList.setVisibility(View.GONE);
    } else{
        noPrev.setVisibility(View.GONE);
        recyclerList.setVisibility(View.VISIBLE);
    }
    return v;
}

@Override
public void onResume() {
    super.onResume();
    h  = new Handler();
    r = () -> {
        BridgeInterface.GetBIlls getBIlls =
                new BillsRequest(getContext());
        getBIlls.request();
        h.postDelayed(r,5000);
    };
    r.run();
}


@Override
public void Bill(List<Bill> bills) {
    list_of_bill = bills;
    initRecycler();
}

public void initRecycler() {
    noPrev.setVisibility(View.GONE);
    recyclerList.setVisibility(View.VISIBLE);
    adapter = new BillRecyclerAdapter(list_of_bill);
    recyclerList.setAdapter(adapter);
    recyclerList.setLayoutManager(new LinearLayoutManager(getContext()));
}
}

这是接口类

public interface BridgeInterface {

interface BillsResponse{
    void Bill(List<Bill> bills);
}

interface GetBIlls{
    void request();
}

}

BillRequest Class; :::

public class BillsRequest implements BridgeInterface.GetBIlls {

private Context context;

private final String url = "http://"+ 
AppSingleton.getInstance().getIP_ADDRESS()
        +"/waterdistrict/bills.php";
private Gson gson;

public BillsRequest(Context context ) {
    this.context = context;
}

@Override
public void request() {
    StringRequest request = new StringRequest(Request.Method.GET, url,
            response -> {
                gson = new GsonBuilder().create();
                List<BillResponse> bill_response =
                        Arrays.asList(gson.fromJson(response, 
 BillResponse.class));
                Log.d("Check", "Levl 2");
                Home.getInstance().Bill(bill_response.get(0).getBills());
            }, error -> {
        Log.d("Error", error.toString());
    });

    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(request);
}
}

0 个答案:

没有答案