走过的坑

蠢不拉几

static 只能开辟一个空间

静态变量和实例变量的区别

实例变量:每次创建对象,都会为每个对象分配成员变量内存空间,实例变量是属于实例对象的,在内存中,创建几次对象,就有几份成员变量。

静态变量:静态变量由于不属于任何实例对象,是属于类的,所以在内存中只会有一份,在类的加载过程中,JVM为静态变量分配一次内存空间。
写了个List<>,结果获取的时候List总是带上之前的。
啊啊啊啊。。。。
竟然写成了
private static final List food = new ArrayList();
正确的如下:

   public List<Food> queryFoodByKind(String kind){
       if(kind.equals("水果")){
           food.add(new Food("水果","个",15,5,2));
           food.add(new Food("水果1","根",15,5,2));
           food.add(new Food("水果2","个",15,5,2));
           food.add(new Food("水果3","颗",15,5,2));
       }else  if(kind.equals("蔬菜")){
           food.add(new Food("蔬菜","个",15,5,2));
           food.add(new Food("蔬菜1","根",15,5,2));
           food.add(new Food("蔬菜2","个",15,5,2));
           food.add(new Food("蔬菜3","颗",15,5,2));
       }

dialogFragment的生命周期

public class RecordFood  extends DialogFragment implements View.OnClickListener {
   private TextView tv_select;
   private TextView unit0;
   private TextView unit1;
   private TextView unit2;
   private TextView nowUnit;
   private EditText weight;
   private RadioGroup radio_rg;
   private String choose;
   private RadioButton radio_g;
   private RadioButton radio_min;
   private RadioButton radio_mid;
   private RadioButton radio_max;

   private Button cancel;
   private Button affirm;
   private Dialog dialog ;


   private String foodname;
   private  String unit;
   public String big;
   private String mid;
   private String small;

   private View mView;

   @Override
   public void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Bundle bundle = getArguments();
       if (bundle != null) {
           foodname =  getArguments().getString("foodname");
           Log.d("QQ", "bundle "+foodname);
           tv_select.setText(foodname);
       }else{
           Log.d("QQ", "bundle null");
       }
   }

   @Override
   @NonNull
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       dialog = new Dialog(getActivity(), R.style.InputCountDialog);
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
       dialog.setContentView(R.layout.recordfood);

       // 设置宽度为屏宽、靠近屏幕底部。
       Window window = dialog.getWindow();
       WindowManager.LayoutParams wlp = window.getAttributes();
       wlp.gravity = Gravity.CENTER;//设置dialog显示居中
//        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
//        wlp.height = WindowManager.LayoutParams.MATCH_PARENT;
//    window.setBackgroundDrawable(new ColorDrawable(Color.BLUE));//注意此处
       window.setLayout(-1,-2);//这2行,和上面的一样,注意顺序就行;
       window.setDimAmount(0.5f);//背景黑暗度
       window.setAttributes(wlp);

       tv_select = dialog.findViewById(R.id.tv_select);
       unit0 = dialog.findViewById(R.id.unit);
       unit1 = dialog.findViewById(R.id.unit1);
       unit2 = dialog.findViewById(R.id.unit2);
       nowUnit = dialog.findViewById(R.id.tv_nowunit);
       weight = (EditText) dialog.findViewById(R.id.et_weight);
       radio_g = (RadioButton) dialog.findViewById(R.id.radio_g);
       radio_min = (RadioButton) dialog.findViewById(R.id.radio_min);

       return dialog;
   }

}

获取foodname 后,在tv_select.setText(foodname);处报如下错误
Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference
但是log却打印出信息。

DialogFragment常用的生命周期函数
,调用顺序如下:
onAttach -->onCreate–>onCreateDialog–>onCreateView–>onViewCreated–>onSaveInstanceState
原因:在onCreate时还没有初始化TextView组件,把tv_select.setText(foodname)放在onCreateDialog之后。

参考:https://www.jianshu.com/p/049082539e1b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值