开发者社区> 问答> 正文

异步请求时,向服务器传参,报空指针异常? 报错

异步请求时,向服务器传参,报空指针异常? 400 报错

public class ExpertFragment extends Fragment implements OnClickListener {
 private View view;
 // private int currentUI = 0; // 0 专家咨询页面 , 1 专家排行页面
 private XListView mLv;
 private LinearLayout mLinearBottom;
 private ImageView mImgLine;
 private TextView mOnlineConsultantTxt, mExpertRankingTxt;
 private ArrayList<ExpertConsult> mConsultList = new ArrayList<ExpertConsult>();
 public ArrayList<ExpertRanking> mRankingList = new ArrayList<ExpertRanking>();
 private ExpertConsultAdapter mConsultAdapter;
 public ExpertRankingAdapter mRankingAdadpter;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  view = inflater.inflate(R.layout.fragment_expert, null);
  initView(view);
  initData();
  return view;

 }

 // 初始化控件以及为其设置监听事件
 private void initView(View view) {
  // mTitle = (TitleView) findViewById(R.id.titleview);
  // mTitle.setBack(new TitleView.ViewBack() {
  // @Override
  // public void onViewBack() {
  // finish();
  // }
  // });
  // mTitle.setTVTitle("在线咨询");

  mLinearBottom = (LinearLayout) view.findViewById(R.id.linearbottom);
  mImgLine = (ImageView) view.findViewById(R.id.imgline);

  mLv = (XListView) view.findViewById(R.id.experlist);
  // 去掉listview下面的线(或者用mConsultLv.setDivider(null);)
  mLv.setDividerHeight(0);
  // 去掉点击listview时,所显示的背景颜色
  mLv.setCacheColorHint(0);

  // mLv.setPullRefreshEnable(true);// 设置下拉刷新
  // mLv.setXListViewListener(this);// 设置监听事件,重写两个方法
  mLv.setPullLoadEnable(false);// 设置上拉刷新

  mOnlineConsultantTxt = (TextView) view
    .findViewById(R.id.txtonlineconsultant);
  mExpertRankingTxt = (TextView) view.findViewById(R.id.txtexpertranking);
  mOnlineConsultantTxt.setOnClickListener(this);
  mExpertRankingTxt.setOnClickListener(this);

  

 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.txtonlineconsultant:
   // mTitle.setTVTitle("在线咨询");
   mOnlineConsultantTxt.setBackgroundColor(getResources().getColor(
     R.color.view_grey));
   mOnlineConsultantTxt.setTextColor(getResources().getColor(
     R.color.main_bg));
   mExpertRankingTxt.setBackgroundColor(getResources().getColor(
     R.color.main_grey));
   mExpertRankingTxt.setTextColor(Color.parseColor("#333333"));
   mImgLine.setVisibility(View.GONE);
   mLinearBottom.setVisibility(View.GONE);
   // 刷新适配器 在线咨询
   if (mConsultList.size() == 0) {
    getOnlineConsultant();
    
   } else {
    mLv.setAdapter(mConsultAdapter);
   }

   break;
  case R.id.txtexpertranking:

   // mTitle.setTVTitle("专家排行");
   mExpertRankingTxt.setBackgroundColor(getResources().getColor(
     R.color.view_grey));
   mExpertRankingTxt.setTextColor(getResources().getColor(
     R.color.main_bg));
   mOnlineConsultantTxt.setBackgroundColor(getResources().getColor(
     R.color.main_grey));
   mOnlineConsultantTxt.setTextColor(Color.parseColor("#333333"));
   mImgLine.setVisibility(View.VISIBLE);
   mLinearBottom.setVisibility(View.VISIBLE);

   // 刷新适配器 专家排行

   if (mRankingList.size() == 0) {
    getExpertRanking();
   } else {
    mLv.setAdapter(mRankingAdadpter);
   }
   break;

  default:
   break;
  }

 }

 public void initData() {
  // TODO Auto-generated method stub
  
  getOnlineConsultant();

 }

 /**
  * 加载更多数据
  */
 private void loadMoreData() {

  mConsultAdapter.mConsultList.get(
    mConsultAdapter.mConsultList.size() - 1).getId();

 }

 private void onLoad() {
  mLv.stopRefresh();
  mLv.stopLoadMore();
  mLv.setRefreshTime(TimeUtil.getTime());
 }

 // 在线咨询
 private void getOnlineConsultant() {
  // lastid 最后一条id

  List<HashMap<String, String>> parameters = MyUtils
    .getNetListParameters(
      new String[] { "lastid" },
      new String[] { mConsultAdapter.mConsultList.get(
        mConsultAdapter.mConsultList.size() - 1)
        .getId() });

  new NetAsyncTask(getActivity(), ExpertConsult.class,
    new TypeToken<List<ExpertConsult>>() {
    }.getType(), new NetAsyncTask.AsyncThreadInter() {
     @Override
     public void onPreExecute() {
      // TODO Auto-generated method stub

     }

     @Override
     public void onExecuteOK(Object o) {
      mConsultList.clear();
      List<ExpertConsult> list = (List<ExpertConsult>) o;
      mConsultList.addAll(list);
      if (mConsultAdapter == null) {
       mConsultAdapter = new ExpertConsultAdapter(
         getActivity(), mConsultList);
      }
      mLv.setAdapter(mConsultAdapter);
      mConsultAdapter.refreshAdapter(mConsultList);

      mLv.setPullLoadEnable(true);

      // mLv.stopLoadMore();
      onLoad();

     }

     @Override
     public void onExecuteFail(String errorInfo) {
      // TODO Auto-generated method stub

     }
    }, parameters).execute("getconsultinglistforpage");

 }

 

 

 // 专家排行
 private void getExpertRanking() {
  // List<HashMap<String, String>> parameters = MyUtils
  // .getNetListParameters(new String[] { "" }, new String[] { "" });

  new NetAsyncTask(getActivity(), ExpertRanking.class,
    new TypeToken<List<ExpertRanking>>() {
    }.getType(), new NetAsyncTask.AsyncThreadInter() {
     @Override
     public void onPreExecute() {
      // TODO Auto-generated method stub

     }

     @Override
     public void onExecuteOK(Object o) {
      // TODO Auto-generated method stub
      mRankingList.clear();
      List<ExpertRanking> list = (List<ExpertRanking>) o;
      mRankingList.addAll(list);
      if (mRankingAdadpter == null) {
       mRankingAdadpter = new ExpertRankingAdapter(
         getActivity(), mRankingList);
      }
      mLv.setAdapter(mRankingAdadpter);
      mRankingAdadpter.refreshAdapter(mRankingList);

     }

     @Override
     public void onExecuteFail(String errorInfo) {
      // TODO Auto-generated method stub

     }
    }, null).execute("getExperterRanking");

 }

}

 

【-------List<HashMap<String, String>> parameters = MyUtils
    .getNetListParameters(
      new String[] { "lastid" },
      new String[] { mConsultAdapter.mConsultList.get(
        mConsultAdapter.mConsultList.size() - 1)
        .getId() });-----------】这段代码报空指针异常

请各位大神们指教!!!!!谢谢各位啦!

 

展开
收起
优选2 2020-06-04 17:38:06 422 0
1 条回答
写回答
取消 提交回答
  • debug模式下,跟踪看看到底是哪个对象是null,很容易就找到问题了######你都不如把异常贴出来……######嗯嗯,谢谢您! ######

    【-------List<HashMap<String, String>> parameters = MyUtils
        .getNetListParameters(
          new String[] { "lastid" },
          new String[] { mConsultAdapter.mConsultList.get(
            mConsultAdapter.mConsultList.size() - 1)
            .getId() });-----------】这段代码报空指针异常

     

     

    就是传参的时候报空指针,冒昧的问一句,可以加一下您的QQ吗?

    ######

    代码这里报空指针异常: List<HashMap<String, String>> parameters = MyUtils
        .getNetListParameters(
          new String[] { "lastid" },
          new String[] { mConsultAdapter.mConsultList.get(
            mConsultAdapter.mConsultList.size() - 1)
            .getId() });



    原因是在这里才放入值:@Override
         public void onExecuteOK(Object o) {
          mConsultList.clear();
          List<ExpertConsult> list = (List<ExpertConsult>) o;
          mConsultList.addAll(list);
          if (mConsultAdapter == null) {
           mConsultAdapter = new ExpertConsultAdapter(
             getActivity(), mConsultList);
          }
          mLv.setAdapter(mConsultAdapter);
          mConsultAdapter.refreshAdapter(mConsultList);

          mLv.setPullLoadEnable(true);

          // mLv.stopLoadMore();
          onLoad();

         }

     

    传参数为:parameters

     

    2020-06-05 13:39:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS计算与存储分离架构实践 立即下载