2

我有两个片段。一个片段,我用另一个片段动态替换第二个片段。从第一个片段中调用新片段中的方法。新片段中的方法为在oncreateView方法中访问的ui元素设置文本。但未访问它显示空指针异常。

代码是:

public class MachinesForm extends Fragment implements OnClickListener {

  TextView ws_name, ws_info, last_update, engine_hours, model_num, ser_num,

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater.inflate(R.layout.fleet_machineinfo, null);
    Log.d("", ""+v);
    ws_name = (TextView) v.findViewById(R.id.txt_info);
    ws_info = (TextView) v.findViewById(R.id.tv_quickinfo);
    return v;
  }

  void setting_machineinfo(int wid, String mid) {
    c = DBAdapter.getMacinfowithws(wid, mid);
    c_hours = DBAdapter.getLastHoursValuesOfMachine(mid);
    c.moveToFirst();
    c_hours.moveToFirst();
    name = c.getString(c.getColumnIndex("WS_Name"));
    System.out.println("name: " + name);//i got name
    Log.d("",""+ws_name);
    ws_name.setText(name); //here shows nullpointer exception
    System.out.println("ok");
  }
}
4

0 回答 0