尝试在null对象上调用方法错误[android]

时间:2015-09-23 04:55:09

标签: java android

当我尝试运行我的应用程序时,我收到一条错误,指出我正在尝试调用null对象上的方法。我知道错误发生在哪里,但我不确定为什么因为我认为我早先初始化了这个对象 代码如下:

var ps = [];
var d1 = $q.defer();
var d2 = $q.defer();
ps.push(d1.promise, d2.promise);

setTimeout(function () {
    d1.reject()
}, 2000)

setTimeout(function () {
    d2.resolve()
}, 5000)

$q.all(ps).then(function () {
    // is not triggered
}).catch(function () {
    //triggered after 2000 ms, I need this triggered after 5000ms
})

错误发生在public class InjuryListFragment extends Fragment{ private RecyclerView mInjuryRecyclerView; private InjuryAdapter mAdapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view=inflater.inflate(R.layout.fragment_injury_list, container, false); mInjuryRecyclerView=(RecyclerView)view.findViewById(R.id.injury_recycler_view); mInjuryRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); updateUI(); return view; } private void updateUI(){ InjuryLab injuryLab=InjuryLab.get(getActivity()); List<Injury>injuries=injuryLab.getInjuries(); mAdapter=new InjuryAdapter(injuries); mInjuryRecyclerView.setAdapter(mAdapter); } private class InjuryHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ private TextView mTitleTextView; private TextView mDescriptionTextView; private Injury mInjury; public InjuryHolder(View itemView){ super(itemView); itemView.setOnClickListener(this); mTitleTextView=(TextView)itemView.findViewById(R.id.list_item_injury_title_text_view); mDescriptionTextView=(TextView)itemView.findViewById(R.id.injury_description); } public void bindInjury(Injury injury){ mInjury=injury; mTitleTextView.setText(mInjury.getTitle()); mDescriptionTextView.setText(mInjury.getDescription()); } @Override public void onClick(View v){ Intent intent=InjuryActivity.newIntent(getActivity(), mInjury.getId()); startActivity(intent); } }

2 个答案:

答案 0 :(得分:0)

仔细检查xml和code中的视图ID。检查是否在初始化TextView之前设置文本。还要检查在执行bindInjury方法之前是否调用了构造函数

答案 1 :(得分:0)

试试这个

public void bindInjury(Injury injury){
  if(injury != null){
    mInjury=injury;
  }
  mTitleTextView.setText(mInjury.getTitle());
  mDescriptionTextView.setText(mInjury.getDescription());
}