首页 > 解决方案 > 我通过 DashboardResponse 的改造得到的 JSON 响应在 textview 上设置什么?

问题描述

nullPointerException当我尝试从响应中将数据设置为文本视图时,我JSON正在接受改造。我想将总销售额、总费用和总购买值设置为 textviews:tv_total_purchase_today、tv_total_sale_today、tv_total_expense_today

我应该在 setsaleInformations 方法中添加什么?

这是我的代码: API 响应

public class DashboardResponse extends CommonResponse {
@Expose
@SerializedName("graph_data")
private ArrayList<Graph> graph_data;
@Expose
@SerializedName("total_sale")
private String total_sale;
@Expose
@SerializedName("total_purchase")
private String total_purchase;
@Expose
@SerializedName("total_expense")
private String total_expense;

public ArrayList<Graph> getGraph_data() {
    return graph_data;
}

public void setGraph_data(ArrayList<Graph> graph_data) {
    this.graph_data = graph_data;
}

public String getTotal_sale() {
    return total_sale;
}

public void setTotal_sale(String total_sale) {
    this.total_sale = total_sale;
}

public String getTotal_purchase() {
    return total_purchase;
}

public void setTotal_purchase(String total_purchase) {
    this.total_purchase = total_purchase;
}

public String getTotal_expense() {
    return total_expense;
}

public void setTotal_expense(String total_expense) {
    this.total_expense = total_expense;
}
}

API 交互器实现

@Override
public void getDashboard(final DashboardListener dashboardListener) {
    Call<DashboardResponse> call = apiServiceInterface.getDashboard();
    call.enqueue(new Callback<DashboardResponse>() {
        @Override
        public void onResponse(Call<DashboardResponse> call, Response<DashboardResponse> response) {
            if(isResponseValid(response) && response.body().isSuccess()){
                dashboardListener.onDashboardFetch(response.body());

            }else{
                dashboardListener.onFailed(prepareFailedMessage(response), APIConstants.DASHBOARD);
            }
        }

仪表板片段

public class DashboardFragment extends Fragment {
private Context context;
private SalesUtils salesUtils;
private DashboardResponse dashboardResponse;
private HomePresenter homePresenter;
private HomeView homeView;

@BindView(R.id.tv_total_sale_today)
TextView tv_total_sale_today;
@BindView(R.id.tv_total_purchase_today)
TextView tv_total_purchase_today;
@BindView(R.id.tv_total_expense_today)
TextView tv_total_expense_today;



@BindView(R.id.cv_sale_today)
CardView cv_sale_today;
@BindView(R.id.cv_purchase_today)
CardView cv_purchase_today;
@BindView(R.id.cv_expense_today)
CardView cv_expense_today;


private ArrayList<Sale> salesToday;
private ArrayList<Product> productSoldToday;



private SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());

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

public static DashboardFragment newInstance(HomePresenter homePresenter) {
    DashboardFragment fragment = new DashboardFragment();
    fragment.initializeSaleInfo(homePresenter);
    return fragment;
}

private void initializeSaleInfo(HomePresenter homePresenter) {
    this.homePresenter = homePresenter;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getActivity();
    homeView = (HomeView) getActivity();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
    ButterKnife.bind(this, rootView);
    cv_sale_today.startAnimation(AnimationUtils.loadSlideInLeftAnimation(context));
    cv_purchase_today.startAnimation(AnimationUtils.loadFadeInAnimation(context));
    cv_expense_today.startAnimation(AnimationUtils.loadSlideInRightAnimation(context));

    homePresenter.fetchDashboard();

    setListeners();
    return rootView;
}




@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    if(homeView!=null) {

        homeView.changeSearchState(false);
    }

}

@Override
public void onPause() {
    super.onPause();
    if(homeView != null)
        homeView.setIsInSaleInfo(false);
}

public void setSaleInformations() {
    if (getActivity() != null && isAdded())
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_total_purchase_today.setText();
                tv_total_sale_today.setText();
                tv_total_expense_today.setText();
            }
        });
}

private Thread resumeSaleInfo = new Thread(new Runnable() {
    @Override
    public void run() {
        salesToday = salesUtils.getSalesDuring(ApplicationUtils.getStartingDateTime(simpleDateTimeFormat.format(System.currentTimeMillis())), ApplicationUtils.getEndingDateTime(simpleDateTimeFormat.format(System.currentTimeMillis())));
        if (getActivity() != null && isAdded())
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_total_purchase_today.setText(ApplicationUtils.getBengaliNumber(salesToday.size() + "") + " " + getString(R.string.buy_today_label));
                    tv_total_sale_today.setText(ApplicationUtils.currencyFormat(SalesUtils.getTotalSale(salesToday)));
                    tv_total_expense_today.setText(ApplicationUtils.getBengaliNumber(SalesUtils.countUnitSold(productSoldToday) + "") + " " + getString(R.string.unit_sold));

                }
            });
    }
});

private void setListeners() {
    cv_sale_today.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            homePresenter.showSaleAt(simpleDateTimeFormat.format(System.currentTimeMillis()));
        }
    });
    cv_purchase_today.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            homePresenter.showSaleAt(simpleDateTimeFormat.format(System.currentTimeMillis()));
        }
    });
    cv_expense_today.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            homePresenter.showUnitSold(simpleDateTimeFormat.format(System.currentTimeMillis()), simpleDateTimeFormat.format(System.currentTimeMillis()));
        }
    });
}

public void setDashboard(DashboardResponse dashboardResponse) {
    Log.e("DASHBOARD", new Gson().toJson(dashboardResponse));
    this.dashboardResponse = dashboardResponse;
    setSaleInformations();
}
}

标签: javaandroidjsonretrofit2

解决方案


这意味着文本视图尚未加载到屏幕上,或者视图尚未初始化。


推荐阅读