2013-11-24 89 views
0

這是我的代碼,當按下一個按鈕的產卵方法被調用: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; 
        } 
       }); 

    } 

在這種方法中,視圖是催生其中有三查框。 我只是把它們放在一個整數作爲關鍵字的哈希表中。原來A是空的,這對我沒有意義。有任何想法嗎?

在此先感謝。

+0

有人可以幫忙嗎? – Sochimickox

+0

B和C怎麼樣?他們很好,或者他們也是空的? –

回答

0

在嘗試訪問作爲其一部分的CheckBox視圖之前,您需要將視圖添加到佈局。在呈現之前訪問這些視圖的任何嘗試都將返回null,因爲它們尚未創建。