2013-11-24 2 views
0

Это мой код, метод Spawn вызывается при нажатии кнопки:CkeckBox имеет карту возвращающегося Null

 public void Spawn (View V) { 
     LayoutInflater inflater = 
     (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View iv = inflater.inflate(R.layout.motor_block, null); 
     //LinearLayout iv = (LinearLayout) findViewById(R.id.motor_block); 
     RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout); 
     CheckBox A = (CheckBox)findViewById(R.id.checkBox1); 
     boxesA.put(currentKey,A); 
     CheckBox B = (CheckBox)findViewById(R.id.checkBox2); 
     boxesB.put(currentKey,B); 
     CheckBox C = (CheckBox)findViewById(R.id.checkBox3); 
     boxesC.put(currentKey,C); 
     iv.setTag(currentKey); 
     rl.addView(iv); 
     currentKey ++; 
      iv.setOnTouchListener(new View.OnTouchListener() { 
       public boolean onTouch(View v,MotionEvent event) { 
         iAction = event.getActionMasked(); 
        switch (iAction) { 
        case MotionEvent.ACTION_MOVE: 
         v.getLocationOnScreen(aLocation);     // get absolute physical location of this View 
         iOffsetX = (aLocation [ 0 ] - (int) v.getX());  // subtract out this View's relative location within its parent View... 
         iOffsetY = (aLocation [ 1 ] - (int) v.getY());  // ...yielding the offsets that convert getRawX/Y's coords to setX/Y's coords 

         iNewX = (int) event.getRawX();      // get absolute physical coords of this touch 
         iNewY = (int) event.getRawY(); 
         iNewX -= iOffsetX;         // remove parent View's screen offset (calc'd above) 
         iNewY -= iOffsetY; 
         iNewX -= iRelX;          // remove stored touch offset 
         iNewY -= iRelY; 
         v.setX(iNewX);          // finally, move View to new coords (relative to its parent View) 
         v.setY(iNewY); 
         bExitValue = true;   
        break; 

        case MotionEvent.ACTION_DOWN: 
         if (delete){ v.setVisibility(View.GONE); } 
         iRelX = (int) event.getX();       // preserve offset of this touch within the View 
         iRelY = (int) event.getY(); 
         bExitValue = false; 
        break; 

        case MotionEvent.ACTION_UP: 
         bExitValue = true; 
        break; 

        } 
        return (bExitValue); 


       } 
      }); 

       iv.setOnLongClickListener(new OnLongClickListener() { 

        @Override 
        public boolean onLongClick(View v) { 
         v.setHapticFeedbackEnabled(false); 
         if (!bExitValue) { 
          Log.i("TAG",(String) v.getTag()); 

          int key = (Integer) v.getTag(); 
          CheckBox A = boxesA.get(key); 
          CheckBox B = boxesB.get(key); 
          CheckBox C = boxesC.get(key); 
         if (A.isChecked()){ //A IS NULL 
          Log.i("Checked","A"); 
         } 
         if (B.isChecked()){ 
          Log.i("Checked","B"); 
         } 
         if (C.isChecked()){ 
          Log.i("Checked","C"); 
         } 

         Log.i("Click","LONG"); 
         v.setHapticFeedbackEnabled(true); 
         } 
         return true; 
        } 
       }); 

    } 

В этом методе View порождена, который имеет три флажков в нем. Я просто поместил их в хэш-карту с целым числом в качестве ключа. Оказывается, что A является нулевым, что не имеет для меня никакого смысла. Есть идеи?

Заранее спасибо.

+0

Может ли кто-нибудь помочь? – Sochimickox

+0

Как насчет B и C? Они в порядке, или они также являются нулевыми? –

ответ

0

Перед попыткой доступа к представлениям CheckBox, которые являются его частью, вам нужно добавить представление в свой макет. Любая попытка доступа к этим представлениям до того, как они были отображены, вернет значение null, поскольку они еще не созданы.

Смежные вопросы